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>
32 #include <linux/uaccess.h>
33 #include <linux/security.h>
36 # include <linux/efi.h>
39 #define DEVMEM_MINOR 1
40 #define DEVPORT_MINOR 4
42 static inline unsigned long size_inside_page(unsigned long start,
47 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
52 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
53 static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
55 return addr + count <= __pa(high_memory);
58 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
64 #ifdef CONFIG_STRICT_DEVMEM
65 static inline int page_is_allowed(unsigned long pfn)
67 return devmem_is_allowed(pfn);
69 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
71 u64 from = ((u64)pfn) << PAGE_SHIFT;
76 if (!devmem_is_allowed(pfn))
84 static inline int page_is_allowed(unsigned long pfn)
88 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
94 #ifndef unxlate_dev_mem_ptr
95 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
96 void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
101 static inline bool should_stop_iteration(void)
105 return fatal_signal_pending(current);
109 * This funcion reads the *physical* memory. The f_pos points directly to the
112 static ssize_t read_mem(struct file *file, char __user *buf,
113 size_t count, loff_t *ppos)
115 phys_addr_t p = *ppos;
124 if (!valid_phys_addr_range(p, count))
127 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
128 /* we don't have page 0 mapped on sparc and m68k.. */
130 sz = size_inside_page(p, count);
132 if (clear_user(buf, sz))
142 bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
147 unsigned long remaining;
150 sz = size_inside_page(p, count);
153 allowed = page_is_allowed(p >> PAGE_SHIFT);
159 /* Show zeros for restricted memory. */
160 remaining = clear_user(buf, sz);
163 * On ia64 if a page has been mapped somewhere as
164 * uncached, then it must also be accessed uncached
165 * by the kernel or data corruption may occur.
167 ptr = xlate_dev_mem_ptr(p);
171 probe = copy_from_kernel_nofault(bounce, ptr, sz);
172 unxlate_dev_mem_ptr(p, ptr);
176 remaining = copy_to_user(buf, bounce, sz);
186 if (should_stop_iteration())
199 static ssize_t write_mem(struct file *file, const char __user *buf,
200 size_t count, loff_t *ppos)
202 phys_addr_t p = *ppos;
204 unsigned long copied;
210 if (!valid_phys_addr_range(p, count))
215 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
216 /* we don't have page 0 mapped on sparc and m68k.. */
218 sz = size_inside_page(p, count);
219 /* Hmm. Do something? */
230 sz = size_inside_page(p, count);
232 allowed = page_is_allowed(p >> PAGE_SHIFT);
236 /* Skip actual writing when a page is marked as restricted. */
239 * On ia64 if a page has been mapped somewhere as
240 * uncached, then it must also be accessed uncached
241 * by the kernel or data corruption may occur.
243 ptr = xlate_dev_mem_ptr(p);
250 copied = copy_from_user(ptr, buf, sz);
251 unxlate_dev_mem_ptr(p, ptr);
253 written += sz - copied;
264 if (should_stop_iteration())
272 int __weak phys_mem_access_prot_allowed(struct file *file,
273 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
278 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
281 * Architectures vary in how they handle caching for addresses
282 * outside of main memory.
285 #ifdef pgprot_noncached
286 static int uncached_access(struct file *file, phys_addr_t addr)
288 #if defined(CONFIG_IA64)
290 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
293 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
296 * Accessing memory above the top the kernel knows about or through a
298 * that was marked O_DSYNC will be done non-cached.
300 if (file->f_flags & O_DSYNC)
302 return addr >= __pa(high_memory);
307 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
308 unsigned long size, pgprot_t vma_prot)
310 #ifdef pgprot_noncached
311 phys_addr_t offset = pfn << PAGE_SHIFT;
313 if (uncached_access(file, offset))
314 return pgprot_noncached(vma_prot);
321 static unsigned long get_unmapped_area_mem(struct file *file,
327 if (!valid_mmap_phys_addr_range(pgoff, len))
328 return (unsigned long) -EINVAL;
329 return pgoff << PAGE_SHIFT;
332 /* permit direct mmap, for read, write or exec */
333 static unsigned memory_mmap_capabilities(struct file *file)
335 return NOMMU_MAP_DIRECT |
336 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
339 static unsigned zero_mmap_capabilities(struct file *file)
341 return NOMMU_MAP_COPY;
344 /* can't do an in-place private mapping if there's no MMU */
345 static inline int private_mapping_ok(struct vm_area_struct *vma)
347 return vma->vm_flags & VM_MAYSHARE;
351 static inline int private_mapping_ok(struct vm_area_struct *vma)
357 static const struct vm_operations_struct mmap_mem_ops = {
358 #ifdef CONFIG_HAVE_IOREMAP_PROT
359 .access = generic_access_phys
363 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
365 size_t size = vma->vm_end - vma->vm_start;
366 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
368 /* Does it even fit in phys_addr_t? */
369 if (offset >> PAGE_SHIFT != vma->vm_pgoff)
372 /* It's illegal to wrap around the end of the physical address space. */
373 if (offset + (phys_addr_t)size - 1 < offset)
376 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
379 if (!private_mapping_ok(vma))
382 if (!range_is_allowed(vma->vm_pgoff, size))
385 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
389 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
393 vma->vm_ops = &mmap_mem_ops;
395 /* Remap-pfn-range will mark the range VM_IO */
396 if (remap_pfn_range(vma,
400 vma->vm_page_prot)) {
406 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
410 /* Turn a kernel-virtual address into a physical page frame */
411 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
414 * RED-PEN: on some architectures there is more mapped memory than
415 * available in mem_map which pfn_valid checks for. Perhaps should add a
418 * RED-PEN: vmalloc is not supported right now.
424 return mmap_mem(file, vma);
428 * This function reads the *virtual* memory as seen by the kernel.
430 static ssize_t read_kmem(struct file *file, char __user *buf,
431 size_t count, loff_t *ppos)
433 unsigned long p = *ppos;
434 ssize_t low_count, read, sz;
435 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
439 if (p < (unsigned long) high_memory) {
441 if (count > (unsigned long)high_memory - p)
442 low_count = (unsigned long)high_memory - p;
444 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
445 /* we don't have page 0 mapped on sparc and m68k.. */
446 if (p < PAGE_SIZE && low_count > 0) {
447 sz = size_inside_page(p, low_count);
448 if (clear_user(buf, sz))
457 while (low_count > 0) {
458 sz = size_inside_page(p, low_count);
461 * On ia64 if a page has been mapped somewhere as
462 * uncached, then it must also be accessed uncached
463 * by the kernel or data corruption may occur
465 kbuf = xlate_dev_kmem_ptr((void *)p);
466 if (!virt_addr_valid(kbuf))
469 if (copy_to_user(buf, kbuf, sz))
476 if (should_stop_iteration()) {
484 kbuf = (char *)__get_free_page(GFP_KERNEL);
488 sz = size_inside_page(p, count);
489 if (!is_vmalloc_or_module_addr((void *)p)) {
493 sz = vread(kbuf, (char *)p, sz);
496 if (copy_to_user(buf, kbuf, sz)) {
504 if (should_stop_iteration())
507 free_page((unsigned long)kbuf);
510 return read ? read : err;
514 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
515 size_t count, loff_t *ppos)
518 unsigned long copied;
521 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
522 /* we don't have page 0 mapped on sparc and m68k.. */
524 sz = size_inside_page(p, count);
525 /* Hmm. Do something? */
536 sz = size_inside_page(p, count);
539 * On ia64 if a page has been mapped somewhere as uncached, then
540 * it must also be accessed uncached by the kernel or data
541 * corruption may occur.
543 ptr = xlate_dev_kmem_ptr((void *)p);
544 if (!virt_addr_valid(ptr))
547 copied = copy_from_user(ptr, buf, sz);
549 written += sz - copied;
558 if (should_stop_iteration())
567 * This function writes to the *virtual* memory as seen by the kernel.
569 static ssize_t write_kmem(struct file *file, const char __user *buf,
570 size_t count, loff_t *ppos)
572 unsigned long p = *ppos;
575 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
578 if (p < (unsigned long) high_memory) {
579 unsigned long to_write = min_t(unsigned long, count,
580 (unsigned long)high_memory - p);
581 wrote = do_write_kmem(p, buf, to_write, ppos);
582 if (wrote != to_write)
590 kbuf = (char *)__get_free_page(GFP_KERNEL);
592 return wrote ? wrote : -ENOMEM;
594 unsigned long sz = size_inside_page(p, count);
597 if (!is_vmalloc_or_module_addr((void *)p)) {
601 n = copy_from_user(kbuf, buf, sz);
606 vwrite(kbuf, (char *)p, sz);
611 if (should_stop_iteration())
614 free_page((unsigned long)kbuf);
618 return virtr + wrote ? : err;
621 static ssize_t read_port(struct file *file, char __user *buf,
622 size_t count, loff_t *ppos)
624 unsigned long i = *ppos;
625 char __user *tmp = buf;
627 if (!access_ok(buf, count))
629 while (count-- > 0 && i < 65536) {
630 if (__put_user(inb(i), tmp) < 0)
639 static ssize_t write_port(struct file *file, const char __user *buf,
640 size_t count, loff_t *ppos)
642 unsigned long i = *ppos;
643 const char __user *tmp = buf;
645 if (!access_ok(buf, count))
647 while (count-- > 0 && i < 65536) {
650 if (__get_user(c, tmp)) {
663 static ssize_t read_null(struct file *file, char __user *buf,
664 size_t count, loff_t *ppos)
669 static ssize_t write_null(struct file *file, const char __user *buf,
670 size_t count, loff_t *ppos)
675 static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
680 static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
682 size_t count = iov_iter_count(from);
683 iov_iter_advance(from, count);
687 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
688 struct splice_desc *sd)
693 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
694 loff_t *ppos, size_t len, unsigned int flags)
696 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
699 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
703 while (iov_iter_count(iter)) {
704 size_t chunk = iov_iter_count(iter), n;
706 if (chunk > PAGE_SIZE)
707 chunk = PAGE_SIZE; /* Just for latency reasons */
708 n = iov_iter_zero(chunk, iter);
709 if (!n && iov_iter_count(iter))
710 return written ? written : -EFAULT;
712 if (signal_pending(current))
713 return written ? written : -ERESTARTSYS;
719 static ssize_t read_zero(struct file *file, char __user *buf,
720 size_t count, loff_t *ppos)
725 size_t chunk = min_t(size_t, count, PAGE_SIZE);
728 left = clear_user(buf + cleared, chunk);
729 if (unlikely(left)) {
730 cleared += (chunk - left);
738 if (signal_pending(current))
746 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
751 if (vma->vm_flags & VM_SHARED)
752 return shmem_zero_setup(vma);
753 vma_set_anonymous(vma);
757 static unsigned long get_unmapped_area_zero(struct file *file,
758 unsigned long addr, unsigned long len,
759 unsigned long pgoff, unsigned long flags)
762 if (flags & MAP_SHARED) {
764 * mmap_zero() will call shmem_zero_setup() to create a file,
765 * so use shmem's get_unmapped_area in case it can be huge;
766 * and pass NULL for file as in mmap.c's get_unmapped_area(),
767 * so as not to confuse shmem with our handle on "/dev/zero".
769 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
772 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
773 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
779 static ssize_t write_full(struct file *file, const char __user *buf,
780 size_t count, loff_t *ppos)
786 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
787 * can fopen() both devices with "a" now. This was previously impossible.
790 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
792 return file->f_pos = 0;
796 * The memory devices use the full 32/64 bits of the offset, and so we cannot
797 * check against negative addresses: they are ok. The return value is weird,
798 * though, in that case (0).
800 * also note that seeking relative to the "end of file" isn't supported:
801 * it has no meaning, so it returns -EINVAL.
803 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
807 inode_lock(file_inode(file));
810 offset += file->f_pos;
813 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
814 if ((unsigned long long)offset >= -MAX_ERRNO) {
818 file->f_pos = offset;
820 force_successful_syscall_return();
825 inode_unlock(file_inode(file));
829 static int open_port(struct inode *inode, struct file *filp)
833 if (!capable(CAP_SYS_RAWIO))
836 rc = security_locked_down(LOCKDOWN_DEV_MEM);
840 if (iminor(inode) != DEVMEM_MINOR)
844 * Use a unified address space to have a single point to manage
845 * revocations when drivers want to take over a /dev/mem mapped
848 filp->f_mapping = iomem_get_mapping();
853 #define zero_lseek null_lseek
854 #define full_lseek null_lseek
855 #define write_zero write_null
856 #define write_iter_zero write_iter_null
857 #define open_mem open_port
858 #define open_kmem open_mem
860 static const struct file_operations __maybe_unused mem_fops = {
861 .llseek = memory_lseek,
867 .get_unmapped_area = get_unmapped_area_mem,
868 .mmap_capabilities = memory_mmap_capabilities,
872 static const struct file_operations __maybe_unused kmem_fops = {
873 .llseek = memory_lseek,
879 .get_unmapped_area = get_unmapped_area_mem,
880 .mmap_capabilities = memory_mmap_capabilities,
884 static const struct file_operations null_fops = {
885 .llseek = null_lseek,
888 .read_iter = read_iter_null,
889 .write_iter = write_iter_null,
890 .splice_write = splice_write_null,
893 static const struct file_operations __maybe_unused port_fops = {
894 .llseek = memory_lseek,
900 static const struct file_operations zero_fops = {
901 .llseek = zero_lseek,
903 .read_iter = read_iter_zero,
905 .write_iter = write_iter_zero,
907 .get_unmapped_area = get_unmapped_area_zero,
909 .mmap_capabilities = zero_mmap_capabilities,
913 static const struct file_operations full_fops = {
914 .llseek = full_lseek,
915 .read_iter = read_iter_zero,
919 static const struct memdev {
922 const struct file_operations *fops;
926 [DEVMEM_MINOR] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
928 #ifdef CONFIG_DEVKMEM
929 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
931 [3] = { "null", 0666, &null_fops, 0 },
932 #ifdef CONFIG_DEVPORT
933 [4] = { "port", 0, &port_fops, 0 },
935 [5] = { "zero", 0666, &zero_fops, 0 },
936 [7] = { "full", 0666, &full_fops, 0 },
937 [8] = { "random", 0666, &random_fops, 0 },
938 [9] = { "urandom", 0666, &urandom_fops, 0 },
940 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
944 static int memory_open(struct inode *inode, struct file *filp)
947 const struct memdev *dev;
949 minor = iminor(inode);
950 if (minor >= ARRAY_SIZE(devlist))
953 dev = &devlist[minor];
957 filp->f_op = dev->fops;
958 filp->f_mode |= dev->fmode;
961 return dev->fops->open(inode, filp);
966 static const struct file_operations memory_fops = {
968 .llseek = noop_llseek,
971 static char *mem_devnode(struct device *dev, umode_t *mode)
973 if (mode && devlist[MINOR(dev->devt)].mode)
974 *mode = devlist[MINOR(dev->devt)].mode;
978 static struct class *mem_class;
980 static int __init chr_dev_init(void)
984 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
985 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
987 mem_class = class_create(THIS_MODULE, "mem");
988 if (IS_ERR(mem_class))
989 return PTR_ERR(mem_class);
991 mem_class->devnode = mem_devnode;
992 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
993 if (!devlist[minor].name)
999 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
1002 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
1003 NULL, devlist[minor].name);
1009 fs_initcall(chr_dev_init);