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 <asm/uaccess.h>
35 # include <linux/efi.h>
38 static inline unsigned long size_inside_page(unsigned long start,
43 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
48 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
49 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
51 return addr + count <= __pa(high_memory);
54 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
60 #ifdef CONFIG_STRICT_DEVMEM
61 static inline int page_is_allowed(unsigned long pfn)
63 return devmem_is_allowed(pfn);
65 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
67 u64 from = ((u64)pfn) << PAGE_SHIFT;
72 if (!devmem_is_allowed(pfn)) {
74 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
75 current->comm, from, to);
84 static inline int page_is_allowed(unsigned long pfn)
88 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
94 void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
99 * This funcion reads the *physical* memory. The f_pos points directly to the
102 static ssize_t read_mem(struct file *file, char __user *buf,
103 size_t count, loff_t *ppos)
105 unsigned long p = *ppos;
109 if (!valid_phys_addr_range(p, count))
112 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
113 /* we don't have page 0 mapped on sparc and m68k.. */
115 sz = size_inside_page(p, count);
117 if (clear_user(buf, sz))
128 unsigned long remaining;
131 sz = size_inside_page(p, count);
133 allowed = page_is_allowed(p >> PAGE_SHIFT);
137 /* Show zeros for restricted memory. */
138 remaining = clear_user(buf, sz);
141 * On ia64 if a page has been mapped somewhere as
142 * uncached, then it must also be accessed uncached
143 * by the kernel or data corruption may occur.
145 ptr = xlate_dev_mem_ptr(p);
149 remaining = copy_to_user(buf, ptr, sz);
151 unxlate_dev_mem_ptr(p, ptr);
167 static ssize_t write_mem(struct file *file, const char __user *buf,
168 size_t count, loff_t *ppos)
170 unsigned long p = *ppos;
172 unsigned long copied;
175 if (!valid_phys_addr_range(p, count))
180 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
181 /* we don't have page 0 mapped on sparc and m68k.. */
183 sz = size_inside_page(p, count);
184 /* Hmm. Do something? */
195 sz = size_inside_page(p, count);
197 allowed = page_is_allowed(p >> PAGE_SHIFT);
201 /* Skip actual writing when a page is marked as restricted. */
204 * On ia64 if a page has been mapped somewhere as
205 * uncached, then it must also be accessed uncached
206 * by the kernel or data corruption may occur.
208 ptr = xlate_dev_mem_ptr(p);
215 copied = copy_from_user(ptr, buf, sz);
216 unxlate_dev_mem_ptr(p, ptr);
218 written += sz - copied;
235 int __weak phys_mem_access_prot_allowed(struct file *file,
236 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
241 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
244 * Architectures vary in how they handle caching for addresses
245 * outside of main memory.
248 #ifdef pgprot_noncached
249 static int uncached_access(struct file *file, unsigned long addr)
251 #if defined(CONFIG_IA64)
253 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
256 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
257 #elif defined(CONFIG_MIPS)
259 extern int __uncached_access(struct file *file,
262 return __uncached_access(file, addr);
266 * Accessing memory above the top the kernel knows about or through a
268 * that was marked O_DSYNC will be done non-cached.
270 if (file->f_flags & O_DSYNC)
272 return addr >= __pa(high_memory);
277 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
278 unsigned long size, pgprot_t vma_prot)
280 #ifdef pgprot_noncached
281 unsigned long offset = pfn << PAGE_SHIFT;
283 if (uncached_access(file, offset))
284 return pgprot_noncached(vma_prot);
291 static unsigned long get_unmapped_area_mem(struct file *file,
297 if (!valid_mmap_phys_addr_range(pgoff, len))
298 return (unsigned long) -EINVAL;
299 return pgoff << PAGE_SHIFT;
302 /* can't do an in-place private mapping if there's no MMU */
303 static inline int private_mapping_ok(struct vm_area_struct *vma)
305 return vma->vm_flags & VM_MAYSHARE;
308 #define get_unmapped_area_mem NULL
310 static inline int private_mapping_ok(struct vm_area_struct *vma)
316 static const struct vm_operations_struct mmap_mem_ops = {
317 #ifdef CONFIG_HAVE_IOREMAP_PROT
318 .access = generic_access_phys
322 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
324 size_t size = vma->vm_end - vma->vm_start;
325 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
327 /* It's illegal to wrap around the end of the physical address space. */
328 if (offset + (phys_addr_t)size - 1 < offset)
331 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
334 if (!private_mapping_ok(vma))
337 if (!range_is_allowed(vma->vm_pgoff, size))
340 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
344 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
348 vma->vm_ops = &mmap_mem_ops;
350 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
351 if (remap_pfn_range(vma,
355 vma->vm_page_prot)) {
361 #ifdef CONFIG_DEVKMEM
362 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
366 /* Turn a kernel-virtual address into a physical page frame */
367 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
370 * RED-PEN: on some architectures there is more mapped memory than
371 * available in mem_map which pfn_valid checks for. Perhaps should add a
374 * RED-PEN: vmalloc is not supported right now.
380 return mmap_mem(file, vma);
384 #ifdef CONFIG_CRASH_DUMP
386 * Read memory corresponding to the old kernel.
388 static ssize_t read_oldmem(struct file *file, char __user *buf,
389 size_t count, loff_t *ppos)
391 unsigned long pfn, offset;
392 size_t read = 0, csize;
396 pfn = *ppos / PAGE_SIZE;
397 if (pfn > saved_max_pfn)
400 offset = (unsigned long)(*ppos % PAGE_SIZE);
401 if (count > PAGE_SIZE - offset)
402 csize = PAGE_SIZE - offset;
406 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
418 #ifdef CONFIG_DEVKMEM
420 * This function reads the *virtual* memory as seen by the kernel.
422 static ssize_t read_kmem(struct file *file, char __user *buf,
423 size_t count, loff_t *ppos)
425 unsigned long p = *ppos;
426 ssize_t low_count, read, sz;
427 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
431 if (p < (unsigned long) high_memory) {
433 if (count > (unsigned long)high_memory - p)
434 low_count = (unsigned long)high_memory - p;
436 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
437 /* we don't have page 0 mapped on sparc and m68k.. */
438 if (p < PAGE_SIZE && low_count > 0) {
439 sz = size_inside_page(p, low_count);
440 if (clear_user(buf, sz))
449 while (low_count > 0) {
450 sz = size_inside_page(p, low_count);
453 * On ia64 if a page has been mapped somewhere as
454 * uncached, then it must also be accessed uncached
455 * by the kernel or data corruption may occur
457 kbuf = xlate_dev_kmem_ptr((char *)p);
459 if (copy_to_user(buf, kbuf, sz))
470 kbuf = (char *)__get_free_page(GFP_KERNEL);
474 sz = size_inside_page(p, count);
475 if (!is_vmalloc_or_module_addr((void *)p)) {
479 sz = vread(kbuf, (char *)p, sz);
482 if (copy_to_user(buf, kbuf, sz)) {
491 free_page((unsigned long)kbuf);
494 return read ? read : err;
498 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
499 size_t count, loff_t *ppos)
502 unsigned long copied;
505 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
506 /* we don't have page 0 mapped on sparc and m68k.. */
508 sz = size_inside_page(p, count);
509 /* Hmm. Do something? */
520 sz = size_inside_page(p, count);
523 * On ia64 if a page has been mapped somewhere as uncached, then
524 * it must also be accessed uncached by the kernel or data
525 * corruption may occur.
527 ptr = xlate_dev_kmem_ptr((char *)p);
529 copied = copy_from_user(ptr, buf, sz);
531 written += sz - copied;
547 * This function writes to the *virtual* memory as seen by the kernel.
549 static ssize_t write_kmem(struct file *file, const char __user *buf,
550 size_t count, loff_t *ppos)
552 unsigned long p = *ppos;
555 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
558 if (p < (unsigned long) high_memory) {
559 unsigned long to_write = min_t(unsigned long, count,
560 (unsigned long)high_memory - p);
561 wrote = do_write_kmem(p, buf, to_write, ppos);
562 if (wrote != to_write)
570 kbuf = (char *)__get_free_page(GFP_KERNEL);
572 return wrote ? wrote : -ENOMEM;
574 unsigned long sz = size_inside_page(p, count);
577 if (!is_vmalloc_or_module_addr((void *)p)) {
581 n = copy_from_user(kbuf, buf, sz);
586 vwrite(kbuf, (char *)p, sz);
592 free_page((unsigned long)kbuf);
596 return virtr + wrote ? : err;
600 #ifdef CONFIG_DEVPORT
601 static ssize_t read_port(struct file *file, char __user *buf,
602 size_t count, loff_t *ppos)
604 unsigned long i = *ppos;
605 char __user *tmp = buf;
607 if (!access_ok(VERIFY_WRITE, buf, count))
609 while (count-- > 0 && i < 65536) {
610 if (__put_user(inb(i), tmp) < 0)
619 static ssize_t write_port(struct file *file, const char __user *buf,
620 size_t count, loff_t *ppos)
622 unsigned long i = *ppos;
623 const char __user * tmp = buf;
625 if (!access_ok(VERIFY_READ, buf, count))
627 while (count-- > 0 && i < 65536) {
629 if (__get_user(c, tmp)) {
643 static ssize_t read_null(struct file *file, char __user *buf,
644 size_t count, loff_t *ppos)
649 static ssize_t write_null(struct file *file, const char __user *buf,
650 size_t count, loff_t *ppos)
655 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
656 struct splice_desc *sd)
661 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
662 loff_t *ppos, size_t len, unsigned int flags)
664 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
667 static ssize_t read_zero(struct file *file, char __user *buf,
668 size_t count, loff_t *ppos)
675 if (!access_ok(VERIFY_WRITE, buf, count))
680 unsigned long unwritten;
681 size_t chunk = count;
683 if (chunk > PAGE_SIZE)
684 chunk = PAGE_SIZE; /* Just for latency reasons */
685 unwritten = __clear_user(buf, chunk);
686 written += chunk - unwritten;
689 if (signal_pending(current))
690 return written ? written : -ERESTARTSYS;
695 return written ? written : -EFAULT;
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->f_path.dentry->d_inode->i_mutex);
739 offset += file->f_pos;
741 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
742 if ((unsigned long long)offset >= ~0xFFFULL) {
746 file->f_pos = offset;
748 force_successful_syscall_return();
753 mutex_unlock(&file->f_path.dentry->d_inode->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 read_full read_zero
766 #define open_mem open_port
767 #define open_kmem open_mem
768 #define open_oldmem open_mem
770 static const struct file_operations mem_fops = {
771 .llseek = memory_lseek,
776 .get_unmapped_area = get_unmapped_area_mem,
779 #ifdef CONFIG_DEVKMEM
780 static const struct file_operations kmem_fops = {
781 .llseek = memory_lseek,
786 .get_unmapped_area = get_unmapped_area_mem,
790 static const struct file_operations null_fops = {
791 .llseek = null_lseek,
794 .splice_write = splice_write_null,
797 #ifdef CONFIG_DEVPORT
798 static const struct file_operations port_fops = {
799 .llseek = memory_lseek,
806 static const struct file_operations zero_fops = {
807 .llseek = zero_lseek,
814 * capabilities for /dev/zero
815 * - permits private mappings, "copies" are taken of the source of zeros
816 * - no writeback happens
818 static struct backing_dev_info zero_bdi = {
820 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
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 = {
833 .llseek = default_llseek,
837 static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
838 unsigned long count, loff_t pos)
842 ssize_t ret = -EFAULT;
843 size_t len = iov_length(iv, count);
845 line = kmalloc(len + 1, GFP_KERNEL);
850 * copy all vectors into a single string, to ensure we do
851 * not interleave our log line with other printk calls
854 for (i = 0; i < count; i++) {
855 if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
861 ret = printk("%s", line);
862 /* printk can add a prefix */
870 static const struct file_operations kmsg_fops = {
871 .aio_write = kmsg_writev,
872 .llseek = noop_llseek,
875 static const struct memdev {
878 const struct file_operations *fops;
879 struct backing_dev_info *dev_info;
881 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
882 #ifdef CONFIG_DEVKMEM
883 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
885 [3] = { "null", 0666, &null_fops, NULL },
886 #ifdef CONFIG_DEVPORT
887 [4] = { "port", 0, &port_fops, NULL },
889 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
890 [7] = { "full", 0666, &full_fops, NULL },
891 [8] = { "random", 0666, &random_fops, NULL },
892 [9] = { "urandom", 0666, &urandom_fops, NULL },
893 [11] = { "kmsg", 0, &kmsg_fops, NULL },
894 #ifdef CONFIG_CRASH_DUMP
895 [12] = { "oldmem", 0, &oldmem_fops, NULL },
899 static int memory_open(struct inode *inode, struct file *filp)
902 const struct memdev *dev;
904 minor = iminor(inode);
905 if (minor >= ARRAY_SIZE(devlist))
908 dev = &devlist[minor];
912 filp->f_op = dev->fops;
914 filp->f_mapping->backing_dev_info = dev->dev_info;
916 /* Is /dev/mem or /dev/kmem ? */
917 if (dev->dev_info == &directly_mappable_cdev_bdi)
918 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
921 return dev->fops->open(inode, filp);
926 static const struct file_operations memory_fops = {
928 .llseek = noop_llseek,
931 static char *mem_devnode(struct device *dev, mode_t *mode)
933 if (mode && devlist[MINOR(dev->devt)].mode)
934 *mode = devlist[MINOR(dev->devt)].mode;
938 static struct class *mem_class;
940 static int __init chr_dev_init(void)
945 err = bdi_init(&zero_bdi);
949 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
950 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
952 mem_class = class_create(THIS_MODULE, "mem");
953 if (IS_ERR(mem_class))
954 return PTR_ERR(mem_class);
956 mem_class->devnode = mem_devnode;
957 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
958 if (!devlist[minor].name)
960 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
961 NULL, devlist[minor].name);
967 fs_initcall(chr_dev_init);