2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
12 #include <linux/miscdevice.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mman.h>
16 #include <linux/random.h>
17 #include <linux/init.h>
18 #include <linux/raw.h>
19 #include <linux/tty.h>
20 #include <linux/capability.h>
21 #include <linux/ptrace.h>
22 #include <linux/device.h>
23 #include <linux/highmem.h>
24 #include <linux/backing-dev.h>
25 #include <linux/splice.h>
26 #include <linux/pfn.h>
27 #include <linux/export.h>
29 #include <linux/uio.h>
31 #include <linux/uaccess.h>
34 # include <linux/efi.h>
37 #define DEVPORT_MINOR 4
39 static inline unsigned long size_inside_page(unsigned long start,
44 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
49 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
50 static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
52 return addr + count <= __pa(high_memory);
55 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
61 #ifdef CONFIG_STRICT_DEVMEM
62 static inline int page_is_allowed(unsigned long pfn)
64 return devmem_is_allowed(pfn);
66 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
68 u64 from = ((u64)pfn) << PAGE_SHIFT;
73 if (!devmem_is_allowed(pfn))
81 static inline int page_is_allowed(unsigned long pfn)
85 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
91 #ifndef unxlate_dev_mem_ptr
92 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
93 void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
98 static inline bool should_stop_iteration(void)
102 return fatal_signal_pending(current);
106 * This funcion reads the *physical* memory. The f_pos points directly to the
109 static ssize_t read_mem(struct file *file, char __user *buf,
110 size_t count, loff_t *ppos)
112 phys_addr_t p = *ppos;
119 if (!valid_phys_addr_range(p, count))
122 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
123 /* we don't have page 0 mapped on sparc and m68k.. */
125 sz = size_inside_page(p, count);
127 if (clear_user(buf, sz))
138 unsigned long remaining;
141 sz = size_inside_page(p, count);
143 allowed = page_is_allowed(p >> PAGE_SHIFT);
147 /* Show zeros for restricted memory. */
148 remaining = clear_user(buf, sz);
151 * On ia64 if a page has been mapped somewhere as
152 * uncached, then it must also be accessed uncached
153 * by the kernel or data corruption may occur.
155 ptr = xlate_dev_mem_ptr(p);
159 remaining = copy_to_user(buf, ptr, sz);
161 unxlate_dev_mem_ptr(p, ptr);
171 if (should_stop_iteration())
179 static ssize_t write_mem(struct file *file, const char __user *buf,
180 size_t count, loff_t *ppos)
182 phys_addr_t p = *ppos;
184 unsigned long copied;
190 if (!valid_phys_addr_range(p, count))
195 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
196 /* we don't have page 0 mapped on sparc and m68k.. */
198 sz = size_inside_page(p, count);
199 /* Hmm. Do something? */
210 sz = size_inside_page(p, count);
212 allowed = page_is_allowed(p >> PAGE_SHIFT);
216 /* Skip actual writing when a page is marked as restricted. */
219 * On ia64 if a page has been mapped somewhere as
220 * uncached, then it must also be accessed uncached
221 * by the kernel or data corruption may occur.
223 ptr = xlate_dev_mem_ptr(p);
230 copied = copy_from_user(ptr, buf, sz);
231 unxlate_dev_mem_ptr(p, ptr);
233 written += sz - copied;
244 if (should_stop_iteration())
252 int __weak phys_mem_access_prot_allowed(struct file *file,
253 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
258 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
261 * Architectures vary in how they handle caching for addresses
262 * outside of main memory.
265 #ifdef pgprot_noncached
266 static int uncached_access(struct file *file, phys_addr_t addr)
268 #if defined(CONFIG_IA64)
270 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
273 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
274 #elif defined(CONFIG_MIPS)
276 extern int __uncached_access(struct file *file,
279 return __uncached_access(file, addr);
283 * Accessing memory above the top the kernel knows about or through a
285 * that was marked O_DSYNC will be done non-cached.
287 if (file->f_flags & O_DSYNC)
289 return addr >= __pa(high_memory);
294 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
295 unsigned long size, pgprot_t vma_prot)
297 #ifdef pgprot_noncached
298 phys_addr_t offset = pfn << PAGE_SHIFT;
300 if (uncached_access(file, offset))
301 return pgprot_noncached(vma_prot);
308 static unsigned long get_unmapped_area_mem(struct file *file,
314 if (!valid_mmap_phys_addr_range(pgoff, len))
315 return (unsigned long) -EINVAL;
316 return pgoff << PAGE_SHIFT;
319 /* permit direct mmap, for read, write or exec */
320 static unsigned memory_mmap_capabilities(struct file *file)
322 return NOMMU_MAP_DIRECT |
323 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
326 static unsigned zero_mmap_capabilities(struct file *file)
328 return NOMMU_MAP_COPY;
331 /* can't do an in-place private mapping if there's no MMU */
332 static inline int private_mapping_ok(struct vm_area_struct *vma)
334 return vma->vm_flags & VM_MAYSHARE;
338 static inline int private_mapping_ok(struct vm_area_struct *vma)
344 static const struct vm_operations_struct mmap_mem_ops = {
345 #ifdef CONFIG_HAVE_IOREMAP_PROT
346 .access = generic_access_phys
350 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
352 size_t size = vma->vm_end - vma->vm_start;
353 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
355 /* It's illegal to wrap around the end of the physical address space. */
356 if (offset + (phys_addr_t)size - 1 < offset)
359 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
362 if (!private_mapping_ok(vma))
365 if (!range_is_allowed(vma->vm_pgoff, size))
368 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
372 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
376 vma->vm_ops = &mmap_mem_ops;
378 /* Remap-pfn-range will mark the range VM_IO */
379 if (remap_pfn_range(vma,
383 vma->vm_page_prot)) {
389 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
393 /* Turn a kernel-virtual address into a physical page frame */
394 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
397 * RED-PEN: on some architectures there is more mapped memory than
398 * available in mem_map which pfn_valid checks for. Perhaps should add a
401 * RED-PEN: vmalloc is not supported right now.
407 return mmap_mem(file, vma);
411 * This function reads the *virtual* memory as seen by the kernel.
413 static ssize_t read_kmem(struct file *file, char __user *buf,
414 size_t count, loff_t *ppos)
416 unsigned long p = *ppos;
417 ssize_t low_count, read, sz;
418 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
422 if (p < (unsigned long) high_memory) {
424 if (count > (unsigned long)high_memory - p)
425 low_count = (unsigned long)high_memory - p;
427 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
428 /* we don't have page 0 mapped on sparc and m68k.. */
429 if (p < PAGE_SIZE && low_count > 0) {
430 sz = size_inside_page(p, low_count);
431 if (clear_user(buf, sz))
440 while (low_count > 0) {
441 sz = size_inside_page(p, low_count);
444 * On ia64 if a page has been mapped somewhere as
445 * uncached, then it must also be accessed uncached
446 * by the kernel or data corruption may occur
448 kbuf = xlate_dev_kmem_ptr((void *)p);
450 if (copy_to_user(buf, kbuf, sz))
457 if (should_stop_iteration()) {
465 kbuf = (char *)__get_free_page(GFP_KERNEL);
469 sz = size_inside_page(p, count);
470 if (!is_vmalloc_or_module_addr((void *)p)) {
474 sz = vread(kbuf, (char *)p, sz);
477 if (copy_to_user(buf, kbuf, sz)) {
485 if (should_stop_iteration())
488 free_page((unsigned long)kbuf);
491 return read ? read : err;
495 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
496 size_t count, loff_t *ppos)
499 unsigned long copied;
502 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
503 /* we don't have page 0 mapped on sparc and m68k.. */
505 sz = size_inside_page(p, count);
506 /* Hmm. Do something? */
517 sz = size_inside_page(p, count);
520 * On ia64 if a page has been mapped somewhere as uncached, then
521 * it must also be accessed uncached by the kernel or data
522 * corruption may occur.
524 ptr = xlate_dev_kmem_ptr((void *)p);
526 copied = copy_from_user(ptr, buf, sz);
528 written += sz - copied;
537 if (should_stop_iteration())
546 * This function writes to the *virtual* memory as seen by the kernel.
548 static ssize_t write_kmem(struct file *file, const char __user *buf,
549 size_t count, loff_t *ppos)
551 unsigned long p = *ppos;
554 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
557 if (p < (unsigned long) high_memory) {
558 unsigned long to_write = min_t(unsigned long, count,
559 (unsigned long)high_memory - p);
560 wrote = do_write_kmem(p, buf, to_write, ppos);
561 if (wrote != to_write)
569 kbuf = (char *)__get_free_page(GFP_KERNEL);
571 return wrote ? wrote : -ENOMEM;
573 unsigned long sz = size_inside_page(p, count);
576 if (!is_vmalloc_or_module_addr((void *)p)) {
580 n = copy_from_user(kbuf, buf, sz);
585 vwrite(kbuf, (char *)p, sz);
590 if (should_stop_iteration())
593 free_page((unsigned long)kbuf);
597 return virtr + wrote ? : err;
600 static ssize_t read_port(struct file *file, char __user *buf,
601 size_t count, loff_t *ppos)
603 unsigned long i = *ppos;
604 char __user *tmp = buf;
606 if (!access_ok(VERIFY_WRITE, buf, count))
608 while (count-- > 0 && i < 65536) {
609 if (__put_user(inb(i), tmp) < 0)
618 static ssize_t write_port(struct file *file, const char __user *buf,
619 size_t count, loff_t *ppos)
621 unsigned long i = *ppos;
622 const char __user *tmp = buf;
624 if (!access_ok(VERIFY_READ, buf, count))
626 while (count-- > 0 && i < 65536) {
629 if (__get_user(c, tmp)) {
642 static ssize_t read_null(struct file *file, char __user *buf,
643 size_t count, loff_t *ppos)
648 static ssize_t write_null(struct file *file, const char __user *buf,
649 size_t count, loff_t *ppos)
654 static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
659 static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
661 size_t count = iov_iter_count(from);
662 iov_iter_advance(from, count);
666 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
667 struct splice_desc *sd)
672 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
673 loff_t *ppos, size_t len, unsigned int flags)
675 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
678 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
682 while (iov_iter_count(iter)) {
683 size_t chunk = iov_iter_count(iter), n;
685 if (chunk > PAGE_SIZE)
686 chunk = PAGE_SIZE; /* Just for latency reasons */
687 n = iov_iter_zero(chunk, iter);
688 if (!n && iov_iter_count(iter))
689 return written ? written : -EFAULT;
691 if (signal_pending(current))
692 return written ? written : -ERESTARTSYS;
698 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
703 if (vma->vm_flags & VM_SHARED)
704 return shmem_zero_setup(vma);
708 static ssize_t write_full(struct file *file, const char __user *buf,
709 size_t count, loff_t *ppos)
715 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
716 * can fopen() both devices with "a" now. This was previously impossible.
719 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
721 return file->f_pos = 0;
725 * The memory devices use the full 32/64 bits of the offset, and so we cannot
726 * check against negative addresses: they are ok. The return value is weird,
727 * though, in that case (0).
729 * also note that seeking relative to the "end of file" isn't supported:
730 * it has no meaning, so it returns -EINVAL.
732 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
736 mutex_lock(&file_inode(file)->i_mutex);
739 offset += file->f_pos;
741 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
742 if (IS_ERR_VALUE((unsigned long long)offset)) {
746 file->f_pos = offset;
748 force_successful_syscall_return();
753 mutex_unlock(&file_inode(file)->i_mutex);
757 static int open_port(struct inode *inode, struct file *filp)
759 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
762 #define zero_lseek null_lseek
763 #define full_lseek null_lseek
764 #define write_zero write_null
765 #define write_iter_zero write_iter_null
766 #define open_mem open_port
767 #define open_kmem open_mem
769 static const struct file_operations __maybe_unused mem_fops = {
770 .llseek = memory_lseek,
776 .get_unmapped_area = get_unmapped_area_mem,
777 .mmap_capabilities = memory_mmap_capabilities,
781 static const struct file_operations __maybe_unused kmem_fops = {
782 .llseek = memory_lseek,
788 .get_unmapped_area = get_unmapped_area_mem,
789 .mmap_capabilities = memory_mmap_capabilities,
793 static const struct file_operations null_fops = {
794 .llseek = null_lseek,
797 .read_iter = read_iter_null,
798 .write_iter = write_iter_null,
799 .splice_write = splice_write_null,
802 static const struct file_operations __maybe_unused port_fops = {
803 .llseek = memory_lseek,
809 static const struct file_operations zero_fops = {
810 .llseek = zero_lseek,
812 .read_iter = read_iter_zero,
813 .write_iter = write_iter_zero,
816 .mmap_capabilities = zero_mmap_capabilities,
820 static const struct file_operations full_fops = {
821 .llseek = full_lseek,
822 .read_iter = read_iter_zero,
826 static const struct memdev {
829 const struct file_operations *fops;
833 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
835 #ifdef CONFIG_DEVKMEM
836 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
838 [3] = { "null", 0666, &null_fops, 0 },
839 #ifdef CONFIG_DEVPORT
840 [4] = { "port", 0, &port_fops, 0 },
842 [5] = { "zero", 0666, &zero_fops, 0 },
843 [7] = { "full", 0666, &full_fops, 0 },
844 [8] = { "random", 0666, &random_fops, 0 },
845 [9] = { "urandom", 0666, &urandom_fops, 0 },
847 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
851 static int memory_open(struct inode *inode, struct file *filp)
854 const struct memdev *dev;
856 minor = iminor(inode);
857 if (minor >= ARRAY_SIZE(devlist))
860 dev = &devlist[minor];
864 filp->f_op = dev->fops;
865 filp->f_mode |= dev->fmode;
868 return dev->fops->open(inode, filp);
873 static const struct file_operations memory_fops = {
875 .llseek = noop_llseek,
878 static char *mem_devnode(struct device *dev, umode_t *mode)
880 if (mode && devlist[MINOR(dev->devt)].mode)
881 *mode = devlist[MINOR(dev->devt)].mode;
885 static struct class *mem_class;
887 static int __init chr_dev_init(void)
891 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
892 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
894 mem_class = class_create(THIS_MODULE, "mem");
895 if (IS_ERR(mem_class))
896 return PTR_ERR(mem_class);
898 mem_class->devnode = mem_devnode;
899 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
900 if (!devlist[minor].name)
906 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
909 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
910 NULL, devlist[minor].name);
916 fs_initcall(chr_dev_init);