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 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
64 #ifdef CONFIG_STRICT_DEVMEM
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 range_is_allowed(unsigned long pfn, unsigned long size)
92 void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
97 * This funcion reads the *physical* memory. The f_pos points directly to the
100 static ssize_t read_mem(struct file *file, char __user *buf,
101 size_t count, loff_t *ppos)
103 phys_addr_t p = *ppos;
107 if (!valid_phys_addr_range(p, count))
110 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
111 /* we don't have page 0 mapped on sparc and m68k.. */
113 sz = size_inside_page(p, count);
115 if (clear_user(buf, sz))
126 unsigned long remaining;
128 sz = size_inside_page(p, count);
130 if (!range_is_allowed(p >> PAGE_SHIFT, count))
134 * On ia64 if a page has been mapped somewhere as uncached, then
135 * it must also be accessed uncached by the kernel or data
136 * corruption may occur.
138 ptr = xlate_dev_mem_ptr(p);
142 remaining = copy_to_user(buf, ptr, sz);
143 unxlate_dev_mem_ptr(p, ptr);
157 static ssize_t write_mem(struct file *file, const char __user *buf,
158 size_t count, loff_t *ppos)
160 phys_addr_t p = *ppos;
162 unsigned long copied;
165 if (!valid_phys_addr_range(p, count))
170 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
171 /* we don't have page 0 mapped on sparc and m68k.. */
173 sz = size_inside_page(p, count);
174 /* Hmm. Do something? */
183 sz = size_inside_page(p, count);
185 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
189 * On ia64 if a page has been mapped somewhere as uncached, then
190 * it must also be accessed uncached by the kernel or data
191 * corruption may occur.
193 ptr = xlate_dev_mem_ptr(p);
200 copied = copy_from_user(ptr, buf, sz);
201 unxlate_dev_mem_ptr(p, ptr);
203 written += sz - copied;
218 #endif /* CONFIG_DEVMEM */
220 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
222 int __weak phys_mem_access_prot_allowed(struct file *file,
223 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
228 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
231 * Architectures vary in how they handle caching for addresses
232 * outside of main memory.
235 #ifdef pgprot_noncached
236 static int uncached_access(struct file *file, phys_addr_t addr)
238 #if defined(CONFIG_IA64)
240 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
243 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
244 #elif defined(CONFIG_MIPS)
246 extern int __uncached_access(struct file *file,
249 return __uncached_access(file, addr);
253 * Accessing memory above the top the kernel knows about or through a
255 * that was marked O_DSYNC will be done non-cached.
257 if (file->f_flags & O_DSYNC)
259 return addr >= __pa(high_memory);
264 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
265 unsigned long size, pgprot_t vma_prot)
267 #ifdef pgprot_noncached
268 phys_addr_t offset = pfn << PAGE_SHIFT;
270 if (uncached_access(file, offset))
271 return pgprot_noncached(vma_prot);
278 static unsigned long get_unmapped_area_mem(struct file *file,
284 if (!valid_mmap_phys_addr_range(pgoff, len))
285 return (unsigned long) -EINVAL;
286 return pgoff << PAGE_SHIFT;
289 /* can't do an in-place private mapping if there's no MMU */
290 static inline int private_mapping_ok(struct vm_area_struct *vma)
292 return vma->vm_flags & VM_MAYSHARE;
295 #define get_unmapped_area_mem NULL
297 static inline int private_mapping_ok(struct vm_area_struct *vma)
303 static const struct vm_operations_struct mmap_mem_ops = {
304 #ifdef CONFIG_HAVE_IOREMAP_PROT
305 .access = generic_access_phys
309 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
311 size_t size = vma->vm_end - vma->vm_start;
313 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
316 if (!private_mapping_ok(vma))
319 if (!range_is_allowed(vma->vm_pgoff, size))
322 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
326 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
330 vma->vm_ops = &mmap_mem_ops;
332 /* Remap-pfn-range will mark the range VM_IO */
333 if (remap_pfn_range(vma,
337 vma->vm_page_prot)) {
342 #endif /* CONFIG_DEVMEM */
344 #ifdef CONFIG_DEVKMEM
345 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
349 /* Turn a kernel-virtual address into a physical page frame */
350 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
353 * RED-PEN: on some architectures there is more mapped memory than
354 * available in mem_map which pfn_valid checks for. Perhaps should add a
357 * RED-PEN: vmalloc is not supported right now.
363 return mmap_mem(file, vma);
367 #ifdef CONFIG_CRASH_DUMP
369 * Read memory corresponding to the old kernel.
371 static ssize_t read_oldmem(struct file *file, char __user *buf,
372 size_t count, loff_t *ppos)
374 unsigned long pfn, offset;
375 size_t read = 0, csize;
379 pfn = *ppos / PAGE_SIZE;
380 if (pfn > saved_max_pfn)
383 offset = (unsigned long)(*ppos % PAGE_SIZE);
384 if (count > PAGE_SIZE - offset)
385 csize = PAGE_SIZE - offset;
389 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
401 #ifdef CONFIG_DEVKMEM
403 * This function reads the *virtual* memory as seen by the kernel.
405 static ssize_t read_kmem(struct file *file, char __user *buf,
406 size_t count, loff_t *ppos)
408 unsigned long p = *ppos;
409 ssize_t low_count, read, sz;
410 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
414 if (p < (unsigned long) high_memory) {
416 if (count > (unsigned long)high_memory - p)
417 low_count = (unsigned long)high_memory - p;
419 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
420 /* we don't have page 0 mapped on sparc and m68k.. */
421 if (p < PAGE_SIZE && low_count > 0) {
422 sz = size_inside_page(p, low_count);
423 if (clear_user(buf, sz))
432 while (low_count > 0) {
433 sz = size_inside_page(p, low_count);
436 * On ia64 if a page has been mapped somewhere as
437 * uncached, then it must also be accessed uncached
438 * by the kernel or data corruption may occur
440 kbuf = xlate_dev_kmem_ptr((char *)p);
442 if (copy_to_user(buf, kbuf, sz))
453 kbuf = (char *)__get_free_page(GFP_KERNEL);
457 sz = size_inside_page(p, count);
458 if (!is_vmalloc_or_module_addr((void *)p)) {
462 sz = vread(kbuf, (char *)p, sz);
465 if (copy_to_user(buf, kbuf, sz)) {
474 free_page((unsigned long)kbuf);
477 return read ? read : err;
481 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
482 size_t count, loff_t *ppos)
485 unsigned long copied;
488 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
489 /* we don't have page 0 mapped on sparc and m68k.. */
491 sz = size_inside_page(p, count);
492 /* Hmm. Do something? */
503 sz = size_inside_page(p, count);
506 * On ia64 if a page has been mapped somewhere as uncached, then
507 * it must also be accessed uncached by the kernel or data
508 * corruption may occur.
510 ptr = xlate_dev_kmem_ptr((char *)p);
512 copied = copy_from_user(ptr, buf, sz);
514 written += sz - copied;
530 * This function writes to the *virtual* memory as seen by the kernel.
532 static ssize_t write_kmem(struct file *file, const char __user *buf,
533 size_t count, loff_t *ppos)
535 unsigned long p = *ppos;
538 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
541 if (p < (unsigned long) high_memory) {
542 unsigned long to_write = min_t(unsigned long, count,
543 (unsigned long)high_memory - p);
544 wrote = do_write_kmem(p, buf, to_write, ppos);
545 if (wrote != to_write)
553 kbuf = (char *)__get_free_page(GFP_KERNEL);
555 return wrote ? wrote : -ENOMEM;
557 unsigned long sz = size_inside_page(p, count);
560 if (!is_vmalloc_or_module_addr((void *)p)) {
564 n = copy_from_user(kbuf, buf, sz);
569 vwrite(kbuf, (char *)p, sz);
575 free_page((unsigned long)kbuf);
579 return virtr + wrote ? : err;
583 #ifdef CONFIG_DEVPORT
584 static ssize_t read_port(struct file *file, char __user *buf,
585 size_t count, loff_t *ppos)
587 unsigned long i = *ppos;
588 char __user *tmp = buf;
590 if (!access_ok(VERIFY_WRITE, buf, count))
592 while (count-- > 0 && i < 65536) {
593 if (__put_user(inb(i), tmp) < 0)
602 static ssize_t write_port(struct file *file, const char __user *buf,
603 size_t count, loff_t *ppos)
605 unsigned long i = *ppos;
606 const char __user *tmp = buf;
608 if (!access_ok(VERIFY_READ, buf, count))
610 while (count-- > 0 && i < 65536) {
612 if (__get_user(c, tmp)) {
626 static ssize_t read_null(struct file *file, char __user *buf,
627 size_t count, loff_t *ppos)
632 static ssize_t write_null(struct file *file, const char __user *buf,
633 size_t count, loff_t *ppos)
638 static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
639 unsigned long nr_segs, loff_t pos)
644 static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
645 unsigned long nr_segs, loff_t pos)
647 return iov_length(iov, nr_segs);
650 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
651 struct splice_desc *sd)
656 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
657 loff_t *ppos, size_t len, unsigned int flags)
659 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
662 static ssize_t read_zero(struct file *file, char __user *buf,
663 size_t count, loff_t *ppos)
670 if (!access_ok(VERIFY_WRITE, buf, count))
675 unsigned long unwritten;
676 size_t chunk = count;
678 if (chunk > PAGE_SIZE)
679 chunk = PAGE_SIZE; /* Just for latency reasons */
680 unwritten = __clear_user(buf, chunk);
681 written += chunk - unwritten;
684 if (signal_pending(current))
685 return written ? written : -ERESTARTSYS;
690 return written ? written : -EFAULT;
693 static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
694 unsigned long nr_segs, loff_t pos)
700 for (i = 0; i < nr_segs; i++) {
701 ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
708 return written ? written : -EFAULT;
711 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
716 if (vma->vm_flags & VM_SHARED)
717 return shmem_zero_setup(vma);
721 static ssize_t write_full(struct file *file, const char __user *buf,
722 size_t count, loff_t *ppos)
728 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
729 * can fopen() both devices with "a" now. This was previously impossible.
732 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
734 return file->f_pos = 0;
737 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
740 * The memory devices use the full 32/64 bits of the offset, and so we cannot
741 * check against negative addresses: they are ok. The return value is weird,
742 * though, in that case (0).
744 * also note that seeking relative to the "end of file" isn't supported:
745 * it has no meaning, so it returns -EINVAL.
747 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
751 mutex_lock(&file_inode(file)->i_mutex);
754 offset += file->f_pos;
756 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
757 if ((unsigned long long)offset >= ~0xFFFULL) {
761 file->f_pos = offset;
763 force_successful_syscall_return();
768 mutex_unlock(&file_inode(file)->i_mutex);
774 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
775 static int open_port(struct inode *inode, struct file *filp)
777 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
781 #define zero_lseek null_lseek
782 #define full_lseek null_lseek
783 #define write_zero write_null
784 #define read_full read_zero
785 #define aio_write_zero aio_write_null
786 #define open_mem open_port
787 #define open_kmem open_mem
788 #define open_oldmem open_mem
791 static const struct file_operations mem_fops = {
792 .llseek = memory_lseek,
797 .get_unmapped_area = get_unmapped_area_mem,
801 #ifdef CONFIG_DEVKMEM
802 static const struct file_operations kmem_fops = {
803 .llseek = memory_lseek,
808 .get_unmapped_area = get_unmapped_area_mem,
812 static const struct file_operations null_fops = {
813 .llseek = null_lseek,
816 .aio_read = aio_read_null,
817 .aio_write = aio_write_null,
818 .splice_write = splice_write_null,
821 #ifdef CONFIG_DEVPORT
822 static const struct file_operations port_fops = {
823 .llseek = memory_lseek,
830 static const struct file_operations zero_fops = {
831 .llseek = zero_lseek,
834 .aio_read = aio_read_zero,
835 .aio_write = aio_write_zero,
840 * capabilities for /dev/zero
841 * - permits private mappings, "copies" are taken of the source of zeros
842 * - no writeback happens
844 static struct backing_dev_info zero_bdi = {
846 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
849 static const struct file_operations full_fops = {
850 .llseek = full_lseek,
855 #ifdef CONFIG_CRASH_DUMP
856 static const struct file_operations oldmem_fops = {
859 .llseek = default_llseek,
863 static const struct memdev {
866 const struct file_operations *fops;
867 struct backing_dev_info *dev_info;
870 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
872 #ifdef CONFIG_DEVKMEM
873 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
875 [3] = { "null", 0666, &null_fops, NULL },
876 #ifdef CONFIG_DEVPORT
877 [4] = { "port", 0, &port_fops, NULL },
879 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
880 [7] = { "full", 0666, &full_fops, NULL },
881 [8] = { "random", 0666, &random_fops, NULL },
882 [9] = { "urandom", 0666, &urandom_fops, NULL },
884 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
886 #ifdef CONFIG_CRASH_DUMP
887 [12] = { "oldmem", 0, &oldmem_fops, NULL },
891 static int memory_open(struct inode *inode, struct file *filp)
894 const struct memdev *dev;
896 minor = iminor(inode);
897 if (minor >= ARRAY_SIZE(devlist))
900 dev = &devlist[minor];
904 filp->f_op = dev->fops;
906 filp->f_mapping->backing_dev_info = dev->dev_info;
908 /* Is /dev/mem or /dev/kmem ? */
909 if (dev->dev_info == &directly_mappable_cdev_bdi)
910 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
913 return dev->fops->open(inode, filp);
918 static const struct file_operations memory_fops = {
920 .llseek = noop_llseek,
923 static char *mem_devnode(struct device *dev, umode_t *mode)
925 if (mode && devlist[MINOR(dev->devt)].mode)
926 *mode = devlist[MINOR(dev->devt)].mode;
930 static struct class *mem_class;
932 static int __init chr_dev_init(void)
937 err = bdi_init(&zero_bdi);
941 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
942 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
944 mem_class = class_create(THIS_MODULE, "mem");
945 if (IS_ERR(mem_class))
946 return PTR_ERR(mem_class);
948 mem_class->devnode = mem_devnode;
949 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
950 if (!devlist[minor].name)
956 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
959 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
960 NULL, devlist[minor].name);
966 fs_initcall(chr_dev_init);