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 mmaping 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/smp_lock.h>
31 #include <asm/uaccess.h>
35 # include <linux/efi.h>
38 static inline unsigned long size_inside_page(unsigned long start,
43 if (-start & (PAGE_SIZE - 1))
44 sz = -start & (PAGE_SIZE - 1);
48 return min_t(unsigned long, sz, size);
52 * Architectures vary in how they handle caching for addresses
53 * outside of main memory.
56 static inline int uncached_access(struct file *file, unsigned long addr)
58 #if defined(CONFIG_IA64)
60 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
62 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
63 #elif defined(CONFIG_MIPS)
65 extern int __uncached_access(struct file *file,
68 return __uncached_access(file, addr);
72 * Accessing memory above the top the kernel knows about or through a file pointer
73 * that was marked O_SYNC will be done non-cached.
75 if (file->f_flags & O_SYNC)
77 return addr >= __pa(high_memory);
81 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
82 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
84 if (addr + count > __pa(high_memory))
90 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
96 #ifdef CONFIG_STRICT_DEVMEM
97 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
99 u64 from = ((u64)pfn) << PAGE_SHIFT;
100 u64 to = from + size;
103 while (cursor < to) {
104 if (!devmem_is_allowed(pfn)) {
106 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
107 current->comm, from, to);
116 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
122 void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
127 * This funcion reads the *physical* memory. The f_pos points directly to the
130 static ssize_t read_mem(struct file * file, char __user * buf,
131 size_t count, loff_t *ppos)
133 unsigned long p = *ppos;
137 if (!valid_phys_addr_range(p, count))
140 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
141 /* we don't have page 0 mapped on sparc and m68k.. */
147 if (clear_user(buf, sz))
159 * Handle first page in case it's not aligned
161 if (-p & (PAGE_SIZE - 1))
162 sz = -p & (PAGE_SIZE - 1);
166 sz = min_t(unsigned long, sz, count);
168 if (!range_is_allowed(p >> PAGE_SHIFT, count))
172 * On ia64 if a page has been mapped somewhere as
173 * uncached, then it must also be accessed uncached
174 * by the kernel or data corruption may occur
176 ptr = xlate_dev_mem_ptr(p);
180 if (copy_to_user(buf, ptr, sz)) {
181 unxlate_dev_mem_ptr(p, ptr);
185 unxlate_dev_mem_ptr(p, ptr);
197 static ssize_t write_mem(struct file * file, const char __user * buf,
198 size_t count, loff_t *ppos)
200 unsigned long p = *ppos;
202 unsigned long copied;
205 if (!valid_phys_addr_range(p, count))
210 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
211 /* we don't have page 0 mapped on sparc and m68k.. */
213 unsigned long sz = PAGE_SIZE - p;
216 /* Hmm. Do something? */
226 * Handle first page in case it's not aligned
228 if (-p & (PAGE_SIZE - 1))
229 sz = -p & (PAGE_SIZE - 1);
233 sz = min_t(unsigned long, sz, count);
235 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
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);
252 written += sz - copied;
253 unxlate_dev_mem_ptr(p, ptr);
259 unxlate_dev_mem_ptr(p, ptr);
271 int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
272 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
277 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
278 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
279 unsigned long size, pgprot_t vma_prot)
281 #ifdef pgprot_noncached
282 unsigned long offset = pfn << PAGE_SHIFT;
284 if (uncached_access(file, offset))
285 return pgprot_noncached(vma_prot);
292 static unsigned long get_unmapped_area_mem(struct file *file,
298 if (!valid_mmap_phys_addr_range(pgoff, len))
299 return (unsigned long) -EINVAL;
300 return pgoff << PAGE_SHIFT;
303 /* can't do an in-place private mapping if there's no MMU */
304 static inline int private_mapping_ok(struct vm_area_struct *vma)
306 return vma->vm_flags & VM_MAYSHARE;
309 #define get_unmapped_area_mem NULL
311 static inline int private_mapping_ok(struct vm_area_struct *vma)
317 static const struct vm_operations_struct mmap_mem_ops = {
318 #ifdef CONFIG_HAVE_IOREMAP_PROT
319 .access = generic_access_phys
323 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
325 size_t size = vma->vm_end - vma->vm_start;
327 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
330 if (!private_mapping_ok(vma))
333 if (!range_is_allowed(vma->vm_pgoff, size))
336 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
340 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
344 vma->vm_ops = &mmap_mem_ops;
346 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
347 if (remap_pfn_range(vma,
351 vma->vm_page_prot)) {
357 #ifdef CONFIG_DEVKMEM
358 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
362 /* Turn a kernel-virtual address into a physical page frame */
363 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
366 * RED-PEN: on some architectures there is more mapped memory
367 * than available in mem_map which pfn_valid checks
368 * for. Perhaps should add a new macro here.
370 * RED-PEN: vmalloc is not supported right now.
376 return mmap_mem(file, vma);
380 #ifdef CONFIG_CRASH_DUMP
382 * Read memory corresponding to the old kernel.
384 static ssize_t read_oldmem(struct file *file, char __user *buf,
385 size_t count, loff_t *ppos)
387 unsigned long pfn, offset;
388 size_t read = 0, csize;
392 pfn = *ppos / PAGE_SIZE;
393 if (pfn > saved_max_pfn)
396 offset = (unsigned long)(*ppos % PAGE_SIZE);
397 if (count > PAGE_SIZE - offset)
398 csize = PAGE_SIZE - offset;
402 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
414 #ifdef CONFIG_DEVKMEM
416 * This function reads the *virtual* memory as seen by the kernel.
418 static ssize_t read_kmem(struct file *file, char __user *buf,
419 size_t count, loff_t *ppos)
421 unsigned long p = *ppos;
422 ssize_t low_count, read, sz;
423 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
426 if (p < (unsigned long) high_memory) {
428 if (count > (unsigned long) high_memory - p)
429 low_count = (unsigned long) high_memory - p;
431 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
432 /* we don't have page 0 mapped on sparc and m68k.. */
433 if (p < PAGE_SIZE && low_count > 0) {
434 size_t tmp = PAGE_SIZE - p;
435 if (tmp > low_count) tmp = low_count;
436 if (clear_user(buf, tmp))
445 while (low_count > 0) {
446 sz = size_inside_page(p, low_count);
449 * On ia64 if a page has been mapped somewhere as
450 * uncached, then it must also be accessed uncached
451 * by the kernel or data corruption may occur
453 kbuf = xlate_dev_kmem_ptr((char *)p);
455 if (copy_to_user(buf, kbuf, sz))
466 kbuf = (char *)__get_free_page(GFP_KERNEL);
470 int len = size_inside_page(p, count);
472 len = vread(kbuf, (char *)p, len);
475 if (copy_to_user(buf, kbuf, len)) {
476 free_page((unsigned long)kbuf);
484 free_page((unsigned long)kbuf);
491 static inline ssize_t
492 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
493 size_t count, loff_t *ppos)
496 unsigned long copied;
499 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
500 /* we don't have page 0 mapped on sparc and m68k.. */
501 if (realp < PAGE_SIZE) {
502 unsigned long sz = PAGE_SIZE - realp;
505 /* Hmm. Do something? */
517 sz = size_inside_page(realp, count);
520 * On ia64 if a page has been mapped somewhere as
521 * uncached, then it must also be accessed uncached
522 * by the kernel or data corruption may occur
524 ptr = xlate_dev_kmem_ptr(p);
526 copied = copy_from_user(ptr, buf, sz);
528 written += sz - copied;
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;
555 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
557 if (p < (unsigned long) high_memory) {
560 if (count > (unsigned long) high_memory - p)
561 wrote = (unsigned long) high_memory - p;
563 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
564 if (written != wrote)
573 kbuf = (char *)__get_free_page(GFP_KERNEL);
575 return wrote ? wrote : -ENOMEM;
577 int len = size_inside_page(p, count);
580 written = copy_from_user(kbuf, buf, len);
584 free_page((unsigned long)kbuf);
588 len = vwrite(kbuf, (char *)p, len);
594 free_page((unsigned long)kbuf);
598 return virtr + wrote;
602 #ifdef CONFIG_DEVPORT
603 static ssize_t read_port(struct file * file, char __user * buf,
604 size_t count, loff_t *ppos)
606 unsigned long i = *ppos;
607 char __user *tmp = buf;
609 if (!access_ok(VERIFY_WRITE, buf, count))
611 while (count-- > 0 && i < 65536) {
612 if (__put_user(inb(i),tmp) < 0)
621 static ssize_t write_port(struct file * file, const char __user * buf,
622 size_t count, loff_t *ppos)
624 unsigned long i = *ppos;
625 const char __user * tmp = buf;
627 if (!access_ok(VERIFY_READ,buf,count))
629 while (count-- > 0 && i < 65536) {
631 if (__get_user(c, tmp)) {
645 static ssize_t read_null(struct file * file, char __user * buf,
646 size_t count, loff_t *ppos)
651 static ssize_t write_null(struct file * file, const char __user * buf,
652 size_t count, loff_t *ppos)
657 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
658 struct splice_desc *sd)
663 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
664 loff_t *ppos, size_t len, unsigned int flags)
666 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
669 static ssize_t read_zero(struct file * file, char __user * buf,
670 size_t count, loff_t *ppos)
677 if (!access_ok(VERIFY_WRITE, buf, count))
682 unsigned long unwritten;
683 size_t chunk = count;
685 if (chunk > PAGE_SIZE)
686 chunk = PAGE_SIZE; /* Just for latency reasons */
687 unwritten = __clear_user(buf, chunk);
688 written += chunk - unwritten;
691 if (signal_pending(current))
692 return written ? written : -ERESTARTSYS;
697 return written ? written : -EFAULT;
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 ssize_t write_full(struct file * file, const char __user * buf,
711 size_t count, loff_t *ppos)
717 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
718 * can fopen() both devices with "a" now. This was previously impossible.
722 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
724 return file->f_pos = 0;
728 * The memory devices use the full 32/64 bits of the offset, and so we cannot
729 * check against negative addresses: they are ok. The return value is weird,
730 * though, in that case (0).
732 * also note that seeking relative to the "end of file" isn't supported:
733 * it has no meaning, so it returns -EINVAL.
735 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
739 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
742 file->f_pos = offset;
744 force_successful_syscall_return();
747 file->f_pos += offset;
749 force_successful_syscall_return();
754 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
758 static int open_port(struct inode * inode, struct file * filp)
760 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
763 #define zero_lseek null_lseek
764 #define full_lseek null_lseek
765 #define write_zero write_null
766 #define read_full read_zero
767 #define open_mem open_port
768 #define open_kmem open_mem
769 #define open_oldmem open_mem
771 static const struct file_operations mem_fops = {
772 .llseek = memory_lseek,
777 .get_unmapped_area = get_unmapped_area_mem,
780 #ifdef CONFIG_DEVKMEM
781 static const struct file_operations kmem_fops = {
782 .llseek = memory_lseek,
787 .get_unmapped_area = get_unmapped_area_mem,
791 static const struct file_operations null_fops = {
792 .llseek = null_lseek,
795 .splice_write = splice_write_null,
798 #ifdef CONFIG_DEVPORT
799 static const struct file_operations port_fops = {
800 .llseek = memory_lseek,
807 static const struct file_operations zero_fops = {
808 .llseek = zero_lseek,
815 * capabilities for /dev/zero
816 * - permits private mappings, "copies" are taken of the source of zeros
818 static struct backing_dev_info zero_bdi = {
820 .capabilities = BDI_CAP_MAP_COPY,
823 static const struct file_operations full_fops = {
824 .llseek = full_lseek,
829 #ifdef CONFIG_CRASH_DUMP
830 static const struct file_operations oldmem_fops = {
836 static ssize_t kmsg_write(struct file * file, const char __user * buf,
837 size_t count, loff_t *ppos)
842 tmp = kmalloc(count + 1, GFP_KERNEL);
846 if (!copy_from_user(tmp, buf, count)) {
848 ret = printk("%s", tmp);
850 /* printk can add a prefix */
857 static const struct file_operations kmsg_fops = {
861 static const struct memdev {
864 const struct file_operations *fops;
865 struct backing_dev_info *dev_info;
867 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
868 #ifdef CONFIG_DEVKMEM
869 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
871 [3] = { "null", 0666, &null_fops, NULL },
872 #ifdef CONFIG_DEVPORT
873 [4] = { "port", 0, &port_fops, NULL },
875 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
876 [7] = { "full", 0666, &full_fops, NULL },
877 [8] = { "random", 0666, &random_fops, NULL },
878 [9] = { "urandom", 0666, &urandom_fops, NULL },
879 [11] = { "kmsg", 0, &kmsg_fops, NULL },
880 #ifdef CONFIG_CRASH_DUMP
881 [12] = { "oldmem", 0, &oldmem_fops, NULL },
885 static int memory_open(struct inode *inode, struct file *filp)
888 const struct memdev *dev;
893 minor = iminor(inode);
894 if (minor >= ARRAY_SIZE(devlist))
897 dev = &devlist[minor];
901 filp->f_op = dev->fops;
903 filp->f_mapping->backing_dev_info = dev->dev_info;
906 ret = dev->fops->open(inode, filp);
914 static const struct file_operations memory_fops = {
918 static char *mem_devnode(struct device *dev, mode_t *mode)
920 if (mode && devlist[MINOR(dev->devt)].mode)
921 *mode = devlist[MINOR(dev->devt)].mode;
925 static struct class *mem_class;
927 static int __init chr_dev_init(void)
932 err = bdi_init(&zero_bdi);
936 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
937 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
939 mem_class = class_create(THIS_MODULE, "mem");
940 mem_class->devnode = mem_devnode;
941 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
942 if (!devlist[minor].name)
944 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
945 NULL, devlist[minor].name);
951 fs_initcall(chr_dev_init);