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 probe
= 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 /* It's illegal to wrap around the end of the physical address space. */
364 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
367 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
370 if (!private_mapping_ok(vma
))
373 if (!range_is_allowed(vma
->vm_pgoff
, size
))
376 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
380 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
384 vma
->vm_ops
= &mmap_mem_ops
;
386 /* Remap-pfn-range will mark the range VM_IO */
387 if (remap_pfn_range(vma
,
391 vma
->vm_page_prot
)) {
397 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
401 /* Turn a kernel-virtual address into a physical page frame */
402 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
405 * RED-PEN: on some architectures there is more mapped memory than
406 * available in mem_map which pfn_valid checks for. Perhaps should add a
409 * RED-PEN: vmalloc is not supported right now.
415 return mmap_mem(file
, vma
);
419 * This function reads the *virtual* memory as seen by the kernel.
421 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
422 size_t count
, loff_t
*ppos
)
424 unsigned long p
= *ppos
;
425 ssize_t low_count
, read
, sz
;
426 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
430 if (p
< (unsigned long) high_memory
) {
432 if (count
> (unsigned long)high_memory
- p
)
433 low_count
= (unsigned long)high_memory
- p
;
435 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
436 /* we don't have page 0 mapped on sparc and m68k.. */
437 if (p
< PAGE_SIZE
&& low_count
> 0) {
438 sz
= size_inside_page(p
, low_count
);
439 if (clear_user(buf
, sz
))
448 while (low_count
> 0) {
449 sz
= size_inside_page(p
, low_count
);
452 * On ia64 if a page has been mapped somewhere as
453 * uncached, then it must also be accessed uncached
454 * by the kernel or data corruption may occur
456 kbuf
= xlate_dev_kmem_ptr((void *)p
);
457 if (!virt_addr_valid(kbuf
))
460 if (copy_to_user(buf
, kbuf
, sz
))
471 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
475 sz
= size_inside_page(p
, count
);
476 if (!is_vmalloc_or_module_addr((void *)p
)) {
480 sz
= vread(kbuf
, (char *)p
, sz
);
483 if (copy_to_user(buf
, kbuf
, sz
)) {
492 free_page((unsigned long)kbuf
);
495 return read
? read
: err
;
499 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
500 size_t count
, loff_t
*ppos
)
503 unsigned long copied
;
506 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
507 /* we don't have page 0 mapped on sparc and m68k.. */
509 sz
= size_inside_page(p
, count
);
510 /* Hmm. Do something? */
521 sz
= size_inside_page(p
, count
);
524 * On ia64 if a page has been mapped somewhere as uncached, then
525 * it must also be accessed uncached by the kernel or data
526 * corruption may occur.
528 ptr
= xlate_dev_kmem_ptr((void *)p
);
529 if (!virt_addr_valid(ptr
))
532 copied
= copy_from_user(ptr
, buf
, sz
);
534 written
+= sz
- copied
;
550 * This function writes to the *virtual* memory as seen by the kernel.
552 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
553 size_t count
, loff_t
*ppos
)
555 unsigned long p
= *ppos
;
558 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
561 if (p
< (unsigned long) high_memory
) {
562 unsigned long to_write
= min_t(unsigned long, count
,
563 (unsigned long)high_memory
- p
);
564 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
565 if (wrote
!= to_write
)
573 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
575 return wrote
? wrote
: -ENOMEM
;
577 unsigned long sz
= size_inside_page(p
, count
);
580 if (!is_vmalloc_or_module_addr((void *)p
)) {
584 n
= copy_from_user(kbuf
, buf
, sz
);
589 vwrite(kbuf
, (char *)p
, sz
);
595 free_page((unsigned long)kbuf
);
599 return virtr
+ wrote
? : err
;
602 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
603 size_t count
, loff_t
*ppos
)
605 unsigned long i
= *ppos
;
606 char __user
*tmp
= buf
;
608 if (!access_ok(VERIFY_WRITE
, buf
, count
))
610 while (count
-- > 0 && i
< 65536) {
611 if (__put_user(inb(i
), tmp
) < 0)
620 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
621 size_t count
, loff_t
*ppos
)
623 unsigned long i
= *ppos
;
624 const char __user
*tmp
= buf
;
626 if (!access_ok(VERIFY_READ
, buf
, count
))
628 while (count
-- > 0 && i
< 65536) {
631 if (__get_user(c
, tmp
)) {
644 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
645 size_t count
, loff_t
*ppos
)
650 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
651 size_t count
, loff_t
*ppos
)
656 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
661 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
663 size_t count
= iov_iter_count(from
);
664 iov_iter_advance(from
, count
);
668 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
669 struct splice_desc
*sd
)
674 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
675 loff_t
*ppos
, size_t len
, unsigned int flags
)
677 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
680 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
684 while (iov_iter_count(iter
)) {
685 size_t chunk
= iov_iter_count(iter
), n
;
687 if (chunk
> PAGE_SIZE
)
688 chunk
= PAGE_SIZE
; /* Just for latency reasons */
689 n
= iov_iter_zero(chunk
, iter
);
690 if (!n
&& iov_iter_count(iter
))
691 return written
? written
: -EFAULT
;
693 if (signal_pending(current
))
694 return written
? written
: -ERESTARTSYS
;
700 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
705 if (vma
->vm_flags
& VM_SHARED
)
706 return shmem_zero_setup(vma
);
710 static unsigned long get_unmapped_area_zero(struct file
*file
,
711 unsigned long addr
, unsigned long len
,
712 unsigned long pgoff
, unsigned long flags
)
715 if (flags
& MAP_SHARED
) {
717 * mmap_zero() will call shmem_zero_setup() to create a file,
718 * so use shmem's get_unmapped_area in case it can be huge;
719 * and pass NULL for file as in mmap.c's get_unmapped_area(),
720 * so as not to confuse shmem with our handle on "/dev/zero".
722 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
725 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
726 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
732 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
733 size_t count
, loff_t
*ppos
)
739 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
740 * can fopen() both devices with "a" now. This was previously impossible.
743 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
745 return file
->f_pos
= 0;
749 * The memory devices use the full 32/64 bits of the offset, and so we cannot
750 * check against negative addresses: they are ok. The return value is weird,
751 * though, in that case (0).
753 * also note that seeking relative to the "end of file" isn't supported:
754 * it has no meaning, so it returns -EINVAL.
756 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
760 inode_lock(file_inode(file
));
763 offset
+= file
->f_pos
;
765 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
766 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
770 file
->f_pos
= offset
;
772 force_successful_syscall_return();
777 inode_unlock(file_inode(file
));
781 static int open_port(struct inode
*inode
, struct file
*filp
)
783 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
786 #define zero_lseek null_lseek
787 #define full_lseek null_lseek
788 #define write_zero write_null
789 #define write_iter_zero write_iter_null
790 #define open_mem open_port
791 #define open_kmem open_mem
793 static const struct file_operations __maybe_unused mem_fops
= {
794 .llseek
= memory_lseek
,
800 .get_unmapped_area
= get_unmapped_area_mem
,
801 .mmap_capabilities
= memory_mmap_capabilities
,
805 static const struct file_operations __maybe_unused kmem_fops
= {
806 .llseek
= memory_lseek
,
812 .get_unmapped_area
= get_unmapped_area_mem
,
813 .mmap_capabilities
= memory_mmap_capabilities
,
817 static const struct file_operations null_fops
= {
818 .llseek
= null_lseek
,
821 .read_iter
= read_iter_null
,
822 .write_iter
= write_iter_null
,
823 .splice_write
= splice_write_null
,
826 static const struct file_operations __maybe_unused port_fops
= {
827 .llseek
= memory_lseek
,
833 static const struct file_operations zero_fops
= {
834 .llseek
= zero_lseek
,
836 .read_iter
= read_iter_zero
,
837 .write_iter
= write_iter_zero
,
839 .get_unmapped_area
= get_unmapped_area_zero
,
841 .mmap_capabilities
= zero_mmap_capabilities
,
845 static const struct file_operations full_fops
= {
846 .llseek
= full_lseek
,
847 .read_iter
= read_iter_zero
,
851 static const struct memdev
{
854 const struct file_operations
*fops
;
858 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
860 #ifdef CONFIG_DEVKMEM
861 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
863 [3] = { "null", 0666, &null_fops
, 0 },
864 #ifdef CONFIG_DEVPORT
865 [4] = { "port", 0, &port_fops
, 0 },
867 [5] = { "zero", 0666, &zero_fops
, 0 },
868 [7] = { "full", 0666, &full_fops
, 0 },
869 [8] = { "random", 0666, &random_fops
, 0 },
870 [9] = { "urandom", 0666, &urandom_fops
, 0 },
872 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
876 static int memory_open(struct inode
*inode
, struct file
*filp
)
879 const struct memdev
*dev
;
881 minor
= iminor(inode
);
882 if (minor
>= ARRAY_SIZE(devlist
))
885 dev
= &devlist
[minor
];
889 filp
->f_op
= dev
->fops
;
890 filp
->f_mode
|= dev
->fmode
;
893 return dev
->fops
->open(inode
, filp
);
898 static const struct file_operations memory_fops
= {
900 .llseek
= noop_llseek
,
903 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
905 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
906 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
910 static struct class *mem_class
;
912 static int __init
chr_dev_init(void)
916 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
917 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
919 mem_class
= class_create(THIS_MODULE
, "mem");
920 if (IS_ERR(mem_class
))
921 return PTR_ERR(mem_class
);
923 mem_class
->devnode
= mem_devnode
;
924 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
925 if (!devlist
[minor
].name
)
931 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
934 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
935 NULL
, devlist
[minor
].name
);
941 fs_initcall(chr_dev_init
);