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>
30 #include <asm/uaccess.h>
34 # include <linux/efi.h>
37 static inline unsigned long size_inside_page(unsigned long start,
42 if (-start & (PAGE_SIZE - 1))
43 sz = -start & (PAGE_SIZE - 1);
47 return min_t(unsigned long, sz, size);
51 * Architectures vary in how they handle caching for addresses
52 * outside of main memory.
55 static inline int uncached_access(struct file *file, unsigned long addr)
57 #if defined(CONFIG_IA64)
59 * On ia64, we ignore O_DSYNC because we cannot tolerate memory attribute aliases.
61 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
62 #elif defined(CONFIG_MIPS)
64 extern int __uncached_access(struct file *file,
67 return __uncached_access(file, addr);
71 * Accessing memory above the top the kernel knows about or through a file pointer
72 * that was marked O_DSYNC will be done non-cached.
74 if (file->f_flags & O_DSYNC)
76 return addr >= __pa(high_memory);
80 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
81 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
83 if (addr + count > __pa(high_memory))
89 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
95 #ifdef CONFIG_STRICT_DEVMEM
96 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
98 u64 from = ((u64)pfn) << PAGE_SHIFT;
102 while (cursor < to) {
103 if (!devmem_is_allowed(pfn)) {
105 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
106 current->comm, from, to);
115 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
121 void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
126 * This funcion reads the *physical* memory. The f_pos points directly to the
129 static ssize_t read_mem(struct file * file, char __user * buf,
130 size_t count, loff_t *ppos)
132 unsigned long p = *ppos;
136 if (!valid_phys_addr_range(p, count))
139 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
140 /* we don't have page 0 mapped on sparc and m68k.. */
146 if (clear_user(buf, sz))
157 unsigned long remaining;
159 sz = size_inside_page(p, count);
161 if (!range_is_allowed(p >> PAGE_SHIFT, count))
165 * On ia64 if a page has been mapped somewhere as
166 * uncached, then it must also be accessed uncached
167 * by the kernel or data corruption may occur
169 ptr = xlate_dev_mem_ptr(p);
173 remaining = copy_to_user(buf, ptr, sz);
174 unxlate_dev_mem_ptr(p, ptr);
188 static ssize_t write_mem(struct file * file, const char __user * buf,
189 size_t count, loff_t *ppos)
191 unsigned long p = *ppos;
193 unsigned long copied;
196 if (!valid_phys_addr_range(p, count))
201 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
202 /* we don't have page 0 mapped on sparc and m68k.. */
204 unsigned long sz = PAGE_SIZE - p;
207 /* Hmm. Do something? */
216 sz = size_inside_page(p, count);
218 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
222 * On ia64 if a page has been mapped somewhere as
223 * uncached, then it must also be accessed uncached
224 * by the kernel or data corruption may occur
226 ptr = xlate_dev_mem_ptr(p);
233 copied = copy_from_user(ptr, buf, sz);
234 unxlate_dev_mem_ptr(p, ptr);
236 written += sz - copied;
252 int __attribute__((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
259 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
260 unsigned long size, pgprot_t vma_prot)
262 #ifdef pgprot_noncached
263 unsigned long offset = pfn << PAGE_SHIFT;
265 if (uncached_access(file, offset))
266 return pgprot_noncached(vma_prot);
273 static unsigned long get_unmapped_area_mem(struct file *file,
279 if (!valid_mmap_phys_addr_range(pgoff, len))
280 return (unsigned long) -EINVAL;
281 return pgoff << PAGE_SHIFT;
284 /* can't do an in-place private mapping if there's no MMU */
285 static inline int private_mapping_ok(struct vm_area_struct *vma)
287 return vma->vm_flags & VM_MAYSHARE;
290 #define get_unmapped_area_mem NULL
292 static inline int private_mapping_ok(struct vm_area_struct *vma)
298 static const struct vm_operations_struct mmap_mem_ops = {
299 #ifdef CONFIG_HAVE_IOREMAP_PROT
300 .access = generic_access_phys
304 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
306 size_t size = vma->vm_end - vma->vm_start;
308 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
311 if (!private_mapping_ok(vma))
314 if (!range_is_allowed(vma->vm_pgoff, size))
317 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
321 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
325 vma->vm_ops = &mmap_mem_ops;
327 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
328 if (remap_pfn_range(vma,
332 vma->vm_page_prot)) {
338 #ifdef CONFIG_DEVKMEM
339 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
343 /* Turn a kernel-virtual address into a physical page frame */
344 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
347 * RED-PEN: on some architectures there is more mapped memory
348 * than available in mem_map which pfn_valid checks
349 * for. Perhaps should add a new macro here.
351 * RED-PEN: vmalloc is not supported right now.
357 return mmap_mem(file, vma);
361 #ifdef CONFIG_CRASH_DUMP
363 * Read memory corresponding to the old kernel.
365 static ssize_t read_oldmem(struct file *file, char __user *buf,
366 size_t count, loff_t *ppos)
368 unsigned long pfn, offset;
369 size_t read = 0, csize;
373 pfn = *ppos / PAGE_SIZE;
374 if (pfn > saved_max_pfn)
377 offset = (unsigned long)(*ppos % PAGE_SIZE);
378 if (count > PAGE_SIZE - offset)
379 csize = PAGE_SIZE - offset;
383 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
395 #ifdef CONFIG_DEVKMEM
397 * This function reads the *virtual* memory as seen by the kernel.
399 static ssize_t read_kmem(struct file *file, char __user *buf,
400 size_t count, loff_t *ppos)
402 unsigned long p = *ppos;
403 ssize_t low_count, read, sz;
404 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 size_t tmp = PAGE_SIZE - p;
416 if (tmp > low_count) tmp = low_count;
417 if (clear_user(buf, tmp))
426 while (low_count > 0) {
427 sz = size_inside_page(p, low_count);
430 * On ia64 if a page has been mapped somewhere as
431 * uncached, then it must also be accessed uncached
432 * by the kernel or data corruption may occur
434 kbuf = xlate_dev_kmem_ptr((char *)p);
436 if (copy_to_user(buf, kbuf, sz))
447 kbuf = (char *)__get_free_page(GFP_KERNEL);
451 int len = size_inside_page(p, count);
453 len = vread(kbuf, (char *)p, len);
456 if (copy_to_user(buf, kbuf, len)) {
457 free_page((unsigned long)kbuf);
465 free_page((unsigned long)kbuf);
472 static inline ssize_t
473 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
474 size_t count, loff_t *ppos)
477 unsigned long copied;
480 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
481 /* we don't have page 0 mapped on sparc and m68k.. */
482 if (realp < PAGE_SIZE) {
483 unsigned long sz = PAGE_SIZE - realp;
486 /* Hmm. Do something? */
498 sz = size_inside_page(realp, count);
501 * On ia64 if a page has been mapped somewhere as
502 * uncached, then it must also be accessed uncached
503 * by the kernel or data corruption may occur
505 ptr = xlate_dev_kmem_ptr(p);
507 copied = copy_from_user(ptr, buf, sz);
509 written += sz - copied;
527 * This function writes to the *virtual* memory as seen by the kernel.
529 static ssize_t write_kmem(struct file * file, const char __user * buf,
530 size_t count, loff_t *ppos)
532 unsigned long p = *ppos;
536 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
538 if (p < (unsigned long) high_memory) {
541 if (count > (unsigned long) high_memory - p)
542 wrote = (unsigned long) high_memory - p;
544 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
545 if (written != wrote)
554 kbuf = (char *)__get_free_page(GFP_KERNEL);
556 return wrote ? wrote : -ENOMEM;
558 int len = size_inside_page(p, count);
560 written = copy_from_user(kbuf, buf, len);
564 free_page((unsigned long)kbuf);
567 len = vwrite(kbuf, (char *)p, len);
573 free_page((unsigned long)kbuf);
577 return virtr + wrote;
581 #ifdef CONFIG_DEVPORT
582 static ssize_t read_port(struct file * file, char __user * buf,
583 size_t count, loff_t *ppos)
585 unsigned long i = *ppos;
586 char __user *tmp = buf;
588 if (!access_ok(VERIFY_WRITE, buf, count))
590 while (count-- > 0 && i < 65536) {
591 if (__put_user(inb(i),tmp) < 0)
600 static ssize_t write_port(struct file * file, const char __user * buf,
601 size_t count, loff_t *ppos)
603 unsigned long i = *ppos;
604 const char __user * tmp = buf;
606 if (!access_ok(VERIFY_READ,buf,count))
608 while (count-- > 0 && i < 65536) {
610 if (__get_user(c, tmp)) {
624 static ssize_t read_null(struct file * file, char __user * buf,
625 size_t count, loff_t *ppos)
630 static ssize_t write_null(struct file * file, const char __user * buf,
631 size_t count, loff_t *ppos)
636 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
637 struct splice_desc *sd)
642 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
643 loff_t *ppos, size_t len, unsigned int flags)
645 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
648 static ssize_t read_zero(struct file * file, char __user * buf,
649 size_t count, loff_t *ppos)
656 if (!access_ok(VERIFY_WRITE, buf, count))
661 unsigned long unwritten;
662 size_t chunk = count;
664 if (chunk > PAGE_SIZE)
665 chunk = PAGE_SIZE; /* Just for latency reasons */
666 unwritten = __clear_user(buf, chunk);
667 written += chunk - unwritten;
670 if (signal_pending(current))
671 return written ? written : -ERESTARTSYS;
676 return written ? written : -EFAULT;
679 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
684 if (vma->vm_flags & VM_SHARED)
685 return shmem_zero_setup(vma);
689 static ssize_t write_full(struct file * file, const char __user * buf,
690 size_t count, loff_t *ppos)
696 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
697 * can fopen() both devices with "a" now. This was previously impossible.
701 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
703 return file->f_pos = 0;
707 * The memory devices use the full 32/64 bits of the offset, and so we cannot
708 * check against negative addresses: they are ok. The return value is weird,
709 * though, in that case (0).
711 * also note that seeking relative to the "end of file" isn't supported:
712 * it has no meaning, so it returns -EINVAL.
714 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
718 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
721 file->f_pos = offset;
723 force_successful_syscall_return();
726 file->f_pos += offset;
728 force_successful_syscall_return();
733 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
737 static int open_port(struct inode * inode, struct file * filp)
739 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
742 #define zero_lseek null_lseek
743 #define full_lseek null_lseek
744 #define write_zero write_null
745 #define read_full read_zero
746 #define open_mem open_port
747 #define open_kmem open_mem
748 #define open_oldmem open_mem
750 static const struct file_operations mem_fops = {
751 .llseek = memory_lseek,
756 .get_unmapped_area = get_unmapped_area_mem,
759 #ifdef CONFIG_DEVKMEM
760 static const struct file_operations kmem_fops = {
761 .llseek = memory_lseek,
766 .get_unmapped_area = get_unmapped_area_mem,
770 static const struct file_operations null_fops = {
771 .llseek = null_lseek,
774 .splice_write = splice_write_null,
777 #ifdef CONFIG_DEVPORT
778 static const struct file_operations port_fops = {
779 .llseek = memory_lseek,
786 static const struct file_operations zero_fops = {
787 .llseek = zero_lseek,
794 * capabilities for /dev/zero
795 * - permits private mappings, "copies" are taken of the source of zeros
797 static struct backing_dev_info zero_bdi = {
799 .capabilities = BDI_CAP_MAP_COPY,
802 static const struct file_operations full_fops = {
803 .llseek = full_lseek,
808 #ifdef CONFIG_CRASH_DUMP
809 static const struct file_operations oldmem_fops = {
815 static ssize_t kmsg_write(struct file * file, const char __user * buf,
816 size_t count, loff_t *ppos)
821 tmp = kmalloc(count + 1, GFP_KERNEL);
825 if (!copy_from_user(tmp, buf, count)) {
827 ret = printk("%s", tmp);
829 /* printk can add a prefix */
836 static const struct file_operations kmsg_fops = {
840 static const struct memdev {
843 const struct file_operations *fops;
844 struct backing_dev_info *dev_info;
846 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
847 #ifdef CONFIG_DEVKMEM
848 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
850 [3] = { "null", 0666, &null_fops, NULL },
851 #ifdef CONFIG_DEVPORT
852 [4] = { "port", 0, &port_fops, NULL },
854 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
855 [7] = { "full", 0666, &full_fops, NULL },
856 [8] = { "random", 0666, &random_fops, NULL },
857 [9] = { "urandom", 0666, &urandom_fops, NULL },
858 [11] = { "kmsg", 0, &kmsg_fops, NULL },
859 #ifdef CONFIG_CRASH_DUMP
860 [12] = { "oldmem", 0, &oldmem_fops, NULL },
864 static int memory_open(struct inode *inode, struct file *filp)
867 const struct memdev *dev;
869 minor = iminor(inode);
870 if (minor >= ARRAY_SIZE(devlist))
873 dev = &devlist[minor];
877 filp->f_op = dev->fops;
879 filp->f_mapping->backing_dev_info = dev->dev_info;
882 return dev->fops->open(inode, filp);
887 static const struct file_operations memory_fops = {
891 static char *mem_devnode(struct device *dev, mode_t *mode)
893 if (mode && devlist[MINOR(dev->devt)].mode)
894 *mode = devlist[MINOR(dev->devt)].mode;
898 static struct class *mem_class;
900 static int __init chr_dev_init(void)
905 err = bdi_init(&zero_bdi);
909 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
910 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
912 mem_class = class_create(THIS_MODULE, "mem");
913 mem_class->devnode = mem_devnode;
914 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
915 if (!devlist[minor].name)
917 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
918 NULL, devlist[minor].name);
924 fs_initcall(chr_dev_init);