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/crash_dump.h>
25 #include <linux/backing-dev.h>
26 #include <linux/bootmem.h>
27 #include <linux/splice.h>
28 #include <linux/pfn.h>
29 #include <linux/export.h>
31 #include <linux/aio.h>
33 #include <asm/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 range_is_allowed(unsigned long pfn, unsigned long size)
66 u64 from = ((u64)pfn) << PAGE_SHIFT;
71 if (!devmem_is_allowed(pfn)) {
73 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
74 current->comm, from, to);
83 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
89 void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
94 * This funcion reads the *physical* memory. The f_pos points directly to the
97 static ssize_t read_mem(struct file *file, char __user *buf,
98 size_t count, loff_t *ppos)
100 phys_addr_t p = *ppos;
104 if (!valid_phys_addr_range(p, count))
107 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
108 /* we don't have page 0 mapped on sparc and m68k.. */
110 sz = size_inside_page(p, count);
112 if (clear_user(buf, sz))
123 unsigned long remaining;
125 sz = size_inside_page(p, count);
127 if (!range_is_allowed(p >> PAGE_SHIFT, count))
131 * On ia64 if a page has been mapped somewhere as uncached, then
132 * it must also be accessed uncached by the kernel or data
133 * corruption may occur.
135 ptr = xlate_dev_mem_ptr(p);
139 remaining = copy_to_user(buf, ptr, sz);
140 unxlate_dev_mem_ptr(p, ptr);
154 static ssize_t write_mem(struct file *file, const char __user *buf,
155 size_t count, loff_t *ppos)
157 phys_addr_t p = *ppos;
159 unsigned long copied;
162 if (!valid_phys_addr_range(p, count))
167 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
168 /* we don't have page 0 mapped on sparc and m68k.. */
170 sz = size_inside_page(p, count);
171 /* Hmm. Do something? */
180 sz = size_inside_page(p, count);
182 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
186 * On ia64 if a page has been mapped somewhere as uncached, then
187 * it must also be accessed uncached by the kernel or data
188 * corruption may occur.
190 ptr = xlate_dev_mem_ptr(p);
197 copied = copy_from_user(ptr, buf, sz);
198 unxlate_dev_mem_ptr(p, ptr);
200 written += sz - copied;
216 int __weak phys_mem_access_prot_allowed(struct file *file,
217 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
222 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
225 * Architectures vary in how they handle caching for addresses
226 * outside of main memory.
229 #ifdef pgprot_noncached
230 static int uncached_access(struct file *file, phys_addr_t addr)
232 #if defined(CONFIG_IA64)
234 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
237 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
238 #elif defined(CONFIG_MIPS)
240 extern int __uncached_access(struct file *file,
243 return __uncached_access(file, addr);
247 * Accessing memory above the top the kernel knows about or through a
249 * that was marked O_DSYNC will be done non-cached.
251 if (file->f_flags & O_DSYNC)
253 return addr >= __pa(high_memory);
258 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
259 unsigned long size, pgprot_t vma_prot)
261 #ifdef pgprot_noncached
262 phys_addr_t offset = pfn << PAGE_SHIFT;
264 if (uncached_access(file, offset))
265 return pgprot_noncached(vma_prot);
272 static unsigned long get_unmapped_area_mem(struct file *file,
278 if (!valid_mmap_phys_addr_range(pgoff, len))
279 return (unsigned long) -EINVAL;
280 return pgoff << PAGE_SHIFT;
283 /* can't do an in-place private mapping if there's no MMU */
284 static inline int private_mapping_ok(struct vm_area_struct *vma)
286 return vma->vm_flags & VM_MAYSHARE;
289 #define get_unmapped_area_mem NULL
291 static inline int private_mapping_ok(struct vm_area_struct *vma)
297 static const struct vm_operations_struct mmap_mem_ops = {
298 #ifdef CONFIG_HAVE_IOREMAP_PROT
299 .access = generic_access_phys
303 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
305 size_t size = vma->vm_end - vma->vm_start;
307 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
310 if (!private_mapping_ok(vma))
313 if (!range_is_allowed(vma->vm_pgoff, size))
316 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
320 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
324 vma->vm_ops = &mmap_mem_ops;
326 /* Remap-pfn-range will mark the range VM_IO */
327 if (remap_pfn_range(vma,
331 vma->vm_page_prot)) {
337 #ifdef CONFIG_DEVKMEM
338 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
342 /* Turn a kernel-virtual address into a physical page frame */
343 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
346 * RED-PEN: on some architectures there is more mapped memory than
347 * available in mem_map which pfn_valid checks for. Perhaps should add a
350 * RED-PEN: vmalloc is not supported right now.
356 return mmap_mem(file, vma);
360 #ifdef CONFIG_CRASH_DUMP
362 * Read memory corresponding to the old kernel.
364 static ssize_t read_oldmem(struct file *file, char __user *buf,
365 size_t count, loff_t *ppos)
367 unsigned long pfn, offset;
368 size_t read = 0, csize;
372 pfn = *ppos / PAGE_SIZE;
373 if (pfn > saved_max_pfn)
376 offset = (unsigned long)(*ppos % PAGE_SIZE);
377 if (count > PAGE_SIZE - offset)
378 csize = PAGE_SIZE - offset;
382 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
394 #ifdef CONFIG_DEVKMEM
396 * This function reads the *virtual* memory as seen by the kernel.
398 static ssize_t read_kmem(struct file *file, char __user *buf,
399 size_t count, loff_t *ppos)
401 unsigned long p = *ppos;
402 ssize_t low_count, read, sz;
403 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
407 if (p < (unsigned long) high_memory) {
409 if (count > (unsigned long)high_memory - p)
410 low_count = (unsigned long)high_memory - p;
412 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
413 /* we don't have page 0 mapped on sparc and m68k.. */
414 if (p < PAGE_SIZE && low_count > 0) {
415 sz = size_inside_page(p, low_count);
416 if (clear_user(buf, sz))
425 while (low_count > 0) {
426 sz = size_inside_page(p, low_count);
429 * On ia64 if a page has been mapped somewhere as
430 * uncached, then it must also be accessed uncached
431 * by the kernel or data corruption may occur
433 kbuf = xlate_dev_kmem_ptr((char *)p);
435 if (copy_to_user(buf, kbuf, sz))
446 kbuf = (char *)__get_free_page(GFP_KERNEL);
450 sz = size_inside_page(p, count);
451 if (!is_vmalloc_or_module_addr((void *)p)) {
455 sz = vread(kbuf, (char *)p, sz);
458 if (copy_to_user(buf, kbuf, sz)) {
467 free_page((unsigned long)kbuf);
470 return read ? read : err;
474 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
475 size_t count, loff_t *ppos)
478 unsigned long copied;
481 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
482 /* we don't have page 0 mapped on sparc and m68k.. */
484 sz = size_inside_page(p, count);
485 /* Hmm. Do something? */
496 sz = size_inside_page(p, count);
499 * On ia64 if a page has been mapped somewhere as uncached, then
500 * it must also be accessed uncached by the kernel or data
501 * corruption may occur.
503 ptr = xlate_dev_kmem_ptr((char *)p);
505 copied = copy_from_user(ptr, buf, sz);
507 written += sz - copied;
523 * This function writes to the *virtual* memory as seen by the kernel.
525 static ssize_t write_kmem(struct file *file, const char __user *buf,
526 size_t count, loff_t *ppos)
528 unsigned long p = *ppos;
531 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
534 if (p < (unsigned long) high_memory) {
535 unsigned long to_write = min_t(unsigned long, count,
536 (unsigned long)high_memory - p);
537 wrote = do_write_kmem(p, buf, to_write, ppos);
538 if (wrote != to_write)
546 kbuf = (char *)__get_free_page(GFP_KERNEL);
548 return wrote ? wrote : -ENOMEM;
550 unsigned long sz = size_inside_page(p, count);
553 if (!is_vmalloc_or_module_addr((void *)p)) {
557 n = copy_from_user(kbuf, buf, sz);
562 vwrite(kbuf, (char *)p, sz);
568 free_page((unsigned long)kbuf);
572 return virtr + wrote ? : err;
576 #ifdef CONFIG_DEVPORT
577 static ssize_t read_port(struct file *file, char __user *buf,
578 size_t count, loff_t *ppos)
580 unsigned long i = *ppos;
581 char __user *tmp = buf;
583 if (!access_ok(VERIFY_WRITE, buf, count))
585 while (count-- > 0 && i < 65536) {
586 if (__put_user(inb(i), tmp) < 0)
595 static ssize_t write_port(struct file *file, const char __user *buf,
596 size_t count, loff_t *ppos)
598 unsigned long i = *ppos;
599 const char __user *tmp = buf;
601 if (!access_ok(VERIFY_READ, buf, count))
603 while (count-- > 0 && i < 65536) {
605 if (__get_user(c, tmp)) {
619 static ssize_t read_null(struct file *file, char __user *buf,
620 size_t count, loff_t *ppos)
625 static ssize_t write_null(struct file *file, const char __user *buf,
626 size_t count, loff_t *ppos)
631 static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
632 unsigned long nr_segs, loff_t pos)
637 static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
638 unsigned long nr_segs, loff_t pos)
640 return iov_length(iov, nr_segs);
643 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
644 struct splice_desc *sd)
649 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
650 loff_t *ppos, size_t len, unsigned int flags)
652 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
655 static ssize_t read_zero(struct file *file, char __user *buf,
656 size_t count, loff_t *ppos)
663 if (!access_ok(VERIFY_WRITE, buf, count))
668 unsigned long unwritten;
669 size_t chunk = count;
671 if (chunk > PAGE_SIZE)
672 chunk = PAGE_SIZE; /* Just for latency reasons */
673 unwritten = __clear_user(buf, chunk);
674 written += chunk - unwritten;
677 if (signal_pending(current))
678 return written ? written : -ERESTARTSYS;
683 return written ? written : -EFAULT;
686 static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
687 unsigned long nr_segs, loff_t pos)
693 for (i = 0; i < nr_segs; i++) {
694 ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
701 return written ? written : -EFAULT;
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 ssize_t write_full(struct file *file, const char __user *buf,
715 size_t count, loff_t *ppos)
721 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
722 * can fopen() both devices with "a" now. This was previously impossible.
725 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
727 return file->f_pos = 0;
731 * The memory devices use the full 32/64 bits of the offset, and so we cannot
732 * check against negative addresses: they are ok. The return value is weird,
733 * though, in that case (0).
735 * also note that seeking relative to the "end of file" isn't supported:
736 * it has no meaning, so it returns -EINVAL.
738 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
742 mutex_lock(&file_inode(file)->i_mutex);
745 offset += file->f_pos;
747 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
748 if ((unsigned long long)offset >= ~0xFFFULL) {
752 file->f_pos = offset;
754 force_successful_syscall_return();
759 mutex_unlock(&file_inode(file)->i_mutex);
763 static int open_port(struct inode *inode, struct file *filp)
765 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
768 #define zero_lseek null_lseek
769 #define full_lseek null_lseek
770 #define write_zero write_null
771 #define read_full read_zero
772 #define aio_write_zero aio_write_null
773 #define open_mem open_port
774 #define open_kmem open_mem
775 #define open_oldmem open_mem
777 static const struct file_operations mem_fops = {
778 .llseek = memory_lseek,
783 .get_unmapped_area = get_unmapped_area_mem,
786 #ifdef CONFIG_DEVKMEM
787 static const struct file_operations kmem_fops = {
788 .llseek = memory_lseek,
793 .get_unmapped_area = get_unmapped_area_mem,
797 static const struct file_operations null_fops = {
798 .llseek = null_lseek,
801 .aio_read = aio_read_null,
802 .aio_write = aio_write_null,
803 .splice_write = splice_write_null,
806 #ifdef CONFIG_DEVPORT
807 static const struct file_operations port_fops = {
808 .llseek = memory_lseek,
815 static const struct file_operations zero_fops = {
816 .llseek = zero_lseek,
819 .aio_read = aio_read_zero,
820 .aio_write = aio_write_zero,
825 * capabilities for /dev/zero
826 * - permits private mappings, "copies" are taken of the source of zeros
827 * - no writeback happens
829 static struct backing_dev_info zero_bdi = {
831 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
834 static const struct file_operations full_fops = {
835 .llseek = full_lseek,
840 #ifdef CONFIG_CRASH_DUMP
841 static const struct file_operations oldmem_fops = {
844 .llseek = default_llseek,
848 static const struct memdev {
851 const struct file_operations *fops;
852 struct backing_dev_info *dev_info;
854 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
855 #ifdef CONFIG_DEVKMEM
856 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
858 [3] = { "null", 0666, &null_fops, NULL },
859 #ifdef CONFIG_DEVPORT
860 [4] = { "port", 0, &port_fops, NULL },
862 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
863 [7] = { "full", 0666, &full_fops, NULL },
864 [8] = { "random", 0666, &random_fops, NULL },
865 [9] = { "urandom", 0666, &urandom_fops, NULL },
867 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
869 #ifdef CONFIG_CRASH_DUMP
870 [12] = { "oldmem", 0, &oldmem_fops, NULL },
874 static int memory_open(struct inode *inode, struct file *filp)
877 const struct memdev *dev;
879 minor = iminor(inode);
880 if (minor >= ARRAY_SIZE(devlist))
883 dev = &devlist[minor];
887 filp->f_op = dev->fops;
889 filp->f_mapping->backing_dev_info = dev->dev_info;
891 /* Is /dev/mem or /dev/kmem ? */
892 if (dev->dev_info == &directly_mappable_cdev_bdi)
893 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
896 return dev->fops->open(inode, filp);
901 static const struct file_operations memory_fops = {
903 .llseek = noop_llseek,
906 static char *mem_devnode(struct device *dev, umode_t *mode)
908 if (mode && devlist[MINOR(dev->devt)].mode)
909 *mode = devlist[MINOR(dev->devt)].mode;
913 static struct class *mem_class;
915 static int __init chr_dev_init(void)
920 err = bdi_init(&zero_bdi);
924 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
925 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
927 mem_class = class_create(THIS_MODULE, "mem");
928 if (IS_ERR(mem_class))
929 return PTR_ERR(mem_class);
931 mem_class->devnode = mem_devnode;
932 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
933 if (!devlist[minor].name)
939 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
942 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
943 NULL, devlist[minor].name);
949 fs_initcall(chr_dev_init);