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 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
97 #ifdef CONFIG_STRICT_DEVMEM
98 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
100 u64 from = ((u64)pfn) << PAGE_SHIFT;
101 u64 to = from + size;
104 while (cursor < to) {
105 if (!devmem_is_allowed(pfn)) {
107 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
108 current->comm, from, to);
117 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
125 void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
130 * This funcion reads the *physical* memory. The f_pos points directly to the
133 static ssize_t read_mem(struct file * file, char __user * buf,
134 size_t count, loff_t *ppos)
136 unsigned long p = *ppos;
140 if (!valid_phys_addr_range(p, count))
143 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
144 /* we don't have page 0 mapped on sparc and m68k.. */
150 if (clear_user(buf, sz))
162 * Handle first page in case it's not aligned
164 if (-p & (PAGE_SIZE - 1))
165 sz = -p & (PAGE_SIZE - 1);
169 sz = min_t(unsigned long, sz, count);
171 if (!range_is_allowed(p >> PAGE_SHIFT, count))
175 * On ia64 if a page has been mapped somewhere as
176 * uncached, then it must also be accessed uncached
177 * by the kernel or data corruption may occur
179 ptr = xlate_dev_mem_ptr(p);
183 if (copy_to_user(buf, ptr, sz)) {
184 unxlate_dev_mem_ptr(p, ptr);
188 unxlate_dev_mem_ptr(p, ptr);
200 static ssize_t write_mem(struct file * file, const char __user * buf,
201 size_t count, loff_t *ppos)
203 unsigned long p = *ppos;
205 unsigned long copied;
208 if (!valid_phys_addr_range(p, count))
213 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
214 /* we don't have page 0 mapped on sparc and m68k.. */
216 unsigned long sz = PAGE_SIZE - p;
219 /* Hmm. Do something? */
229 * Handle first page in case it's not aligned
231 if (-p & (PAGE_SIZE - 1))
232 sz = -p & (PAGE_SIZE - 1);
236 sz = min_t(unsigned long, sz, count);
238 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
242 * On ia64 if a page has been mapped somewhere as
243 * uncached, then it must also be accessed uncached
244 * by the kernel or data corruption may occur
246 ptr = xlate_dev_mem_ptr(p);
253 copied = copy_from_user(ptr, buf, sz);
255 written += sz - copied;
256 unxlate_dev_mem_ptr(p, ptr);
262 unxlate_dev_mem_ptr(p, ptr);
273 #endif /* CONFIG_DEVMEM */
275 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
277 int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
278 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
283 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
284 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
285 unsigned long size, pgprot_t vma_prot)
287 #ifdef pgprot_noncached
288 unsigned long offset = pfn << PAGE_SHIFT;
290 if (uncached_access(file, offset))
291 return pgprot_noncached(vma_prot);
298 static unsigned long get_unmapped_area_mem(struct file *file,
304 if (!valid_mmap_phys_addr_range(pgoff, len))
305 return (unsigned long) -EINVAL;
306 return pgoff << PAGE_SHIFT;
309 /* can't do an in-place private mapping if there's no MMU */
310 static inline int private_mapping_ok(struct vm_area_struct *vma)
312 return vma->vm_flags & VM_MAYSHARE;
315 #define get_unmapped_area_mem NULL
317 static inline int private_mapping_ok(struct vm_area_struct *vma)
323 static const struct vm_operations_struct mmap_mem_ops = {
324 #ifdef CONFIG_HAVE_IOREMAP_PROT
325 .access = generic_access_phys
329 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
331 size_t size = vma->vm_end - vma->vm_start;
333 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
336 if (!private_mapping_ok(vma))
339 if (!range_is_allowed(vma->vm_pgoff, size))
342 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
346 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
350 vma->vm_ops = &mmap_mem_ops;
352 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
353 if (remap_pfn_range(vma,
357 vma->vm_page_prot)) {
362 #endif /* CONFIG_DEVMEM */
364 #ifdef CONFIG_DEVKMEM
365 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
369 /* Turn a kernel-virtual address into a physical page frame */
370 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
373 * RED-PEN: on some architectures there is more mapped memory
374 * than available in mem_map which pfn_valid checks
375 * for. Perhaps should add a new macro here.
377 * RED-PEN: vmalloc is not supported right now.
383 return mmap_mem(file, vma);
387 #ifdef CONFIG_CRASH_DUMP
389 * Read memory corresponding to the old kernel.
391 static ssize_t read_oldmem(struct file *file, char __user *buf,
392 size_t count, loff_t *ppos)
394 unsigned long pfn, offset;
395 size_t read = 0, csize;
399 pfn = *ppos / PAGE_SIZE;
400 if (pfn > saved_max_pfn)
403 offset = (unsigned long)(*ppos % PAGE_SIZE);
404 if (count > PAGE_SIZE - offset)
405 csize = PAGE_SIZE - offset;
409 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
421 #ifdef CONFIG_DEVKMEM
423 * This function reads the *virtual* memory as seen by the kernel.
425 static ssize_t read_kmem(struct file *file, char __user *buf,
426 size_t count, loff_t *ppos)
428 unsigned long p = *ppos;
429 ssize_t low_count, read, sz;
430 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
434 if (p < (unsigned long) high_memory) {
436 if (count > (unsigned long) high_memory - p)
437 low_count = (unsigned long) high_memory - p;
439 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
440 /* we don't have page 0 mapped on sparc and m68k.. */
441 if (p < PAGE_SIZE && low_count > 0) {
442 size_t tmp = PAGE_SIZE - p;
443 if (tmp > low_count) tmp = low_count;
444 if (clear_user(buf, tmp))
453 while (low_count > 0) {
454 sz = size_inside_page(p, low_count);
457 * On ia64 if a page has been mapped somewhere as
458 * uncached, then it must also be accessed uncached
459 * by the kernel or data corruption may occur
461 kbuf = xlate_dev_kmem_ptr((char *)p);
463 if (copy_to_user(buf, kbuf, sz))
474 kbuf = (char *)__get_free_page(GFP_KERNEL);
478 int len = size_inside_page(p, count);
480 if (!is_vmalloc_or_module_addr((void *)p)) {
484 len = vread(kbuf, (char *)p, len);
487 if (copy_to_user(buf, kbuf, len)) {
496 free_page((unsigned long)kbuf);
499 return read ? read : err;
503 static inline ssize_t
504 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
505 size_t count, loff_t *ppos)
508 unsigned long copied;
511 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
512 /* we don't have page 0 mapped on sparc and m68k.. */
513 if (realp < PAGE_SIZE) {
514 unsigned long sz = PAGE_SIZE - realp;
517 /* Hmm. Do something? */
529 sz = size_inside_page(realp, count);
532 * On ia64 if a page has been mapped somewhere as
533 * uncached, then it must also be accessed uncached
534 * by the kernel or data corruption may occur
536 ptr = xlate_dev_kmem_ptr(p);
538 copied = copy_from_user(ptr, buf, sz);
540 written += sz - copied;
558 * This function writes to the *virtual* memory as seen by the kernel.
560 static ssize_t write_kmem(struct file * file, const char __user * buf,
561 size_t count, loff_t *ppos)
563 unsigned long p = *ppos;
567 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
570 if (p < (unsigned long) high_memory) {
573 if (count > (unsigned long) high_memory - p)
574 wrote = (unsigned long) high_memory - p;
576 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
577 if (written != wrote)
586 kbuf = (char *)__get_free_page(GFP_KERNEL);
588 return wrote ? wrote : -ENOMEM;
590 int len = size_inside_page(p, count);
592 if (!is_vmalloc_or_module_addr((void *)p)) {
597 written = copy_from_user(kbuf, buf, len);
603 vwrite(kbuf, (char *)p, len);
609 free_page((unsigned long)kbuf);
613 return virtr + wrote ? : err;
617 #ifdef CONFIG_DEVPORT
618 static ssize_t read_port(struct file * file, char __user * buf,
619 size_t count, loff_t *ppos)
621 unsigned long i = *ppos;
622 char __user *tmp = buf;
624 if (!access_ok(VERIFY_WRITE, buf, count))
626 while (count-- > 0 && i < 65536) {
627 if (__put_user(inb(i),tmp) < 0)
636 static ssize_t write_port(struct file * file, const char __user * buf,
637 size_t count, loff_t *ppos)
639 unsigned long i = *ppos;
640 const char __user * tmp = buf;
642 if (!access_ok(VERIFY_READ,buf,count))
644 while (count-- > 0 && i < 65536) {
646 if (__get_user(c, tmp)) {
660 static ssize_t read_null(struct file * file, char __user * buf,
661 size_t count, loff_t *ppos)
666 static ssize_t write_null(struct file * file, const char __user * buf,
667 size_t count, loff_t *ppos)
672 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
673 struct splice_desc *sd)
678 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
679 loff_t *ppos, size_t len, unsigned int flags)
681 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
684 static ssize_t read_zero(struct file * file, char __user * buf,
685 size_t count, loff_t *ppos)
692 if (!access_ok(VERIFY_WRITE, buf, count))
697 unsigned long unwritten;
698 size_t chunk = count;
700 if (chunk > PAGE_SIZE)
701 chunk = PAGE_SIZE; /* Just for latency reasons */
702 unwritten = __clear_user(buf, chunk);
703 written += chunk - unwritten;
706 if (signal_pending(current))
707 return written ? written : -ERESTARTSYS;
712 return written ? written : -EFAULT;
715 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
720 if (vma->vm_flags & VM_SHARED)
721 return shmem_zero_setup(vma);
725 static ssize_t write_full(struct file * file, const char __user * buf,
726 size_t count, loff_t *ppos)
732 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
733 * can fopen() both devices with "a" now. This was previously impossible.
737 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
739 return file->f_pos = 0;
742 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
745 * The memory devices use the full 32/64 bits of the offset, and so we cannot
746 * check against negative addresses: they are ok. The return value is weird,
747 * though, in that case (0).
749 * also note that seeking relative to the "end of file" isn't supported:
750 * it has no meaning, so it returns -EINVAL.
752 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
756 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
759 file->f_pos = offset;
761 force_successful_syscall_return();
764 file->f_pos += offset;
766 force_successful_syscall_return();
771 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
777 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
778 static int open_port(struct inode * inode, struct file * filp)
780 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
784 #define zero_lseek null_lseek
785 #define full_lseek null_lseek
786 #define write_zero write_null
787 #define read_full read_zero
788 #define open_mem open_port
789 #define open_kmem open_mem
790 #define open_oldmem open_mem
793 static const struct file_operations mem_fops = {
794 .llseek = memory_lseek,
799 .get_unmapped_area = get_unmapped_area_mem,
803 #ifdef CONFIG_DEVKMEM
804 static const struct file_operations kmem_fops = {
805 .llseek = memory_lseek,
810 .get_unmapped_area = get_unmapped_area_mem,
814 static const struct file_operations null_fops = {
815 .llseek = null_lseek,
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,
838 * capabilities for /dev/zero
839 * - permits private mappings, "copies" are taken of the source of zeros
840 * - no writeback happens
842 static struct backing_dev_info zero_bdi = {
844 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
847 static const struct file_operations full_fops = {
848 .llseek = full_lseek,
853 #ifdef CONFIG_CRASH_DUMP
854 static const struct file_operations oldmem_fops = {
860 static ssize_t kmsg_write(struct file * file, const char __user * buf,
861 size_t count, loff_t *ppos)
866 tmp = kmalloc(count + 1, GFP_KERNEL);
870 if (!copy_from_user(tmp, buf, count)) {
872 ret = printk("%s", tmp);
874 /* printk can add a prefix */
881 static const struct file_operations kmsg_fops = {
885 static const struct memdev {
888 const struct file_operations *fops;
889 struct backing_dev_info *dev_info;
892 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
894 #ifdef CONFIG_DEVKMEM
895 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
897 [3] = { "null", 0666, &null_fops, NULL },
898 #ifdef CONFIG_DEVPORT
899 [4] = { "port", 0, &port_fops, NULL },
901 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
902 [7] = { "full", 0666, &full_fops, NULL },
903 [8] = { "random", 0666, &random_fops, NULL },
904 [9] = { "urandom", 0666, &urandom_fops, NULL },
905 [11] = { "kmsg", 0, &kmsg_fops, NULL },
906 #ifdef CONFIG_CRASH_DUMP
907 [12] = { "oldmem", 0, &oldmem_fops, NULL },
911 static int memory_open(struct inode *inode, struct file *filp)
914 const struct memdev *dev;
919 minor = iminor(inode);
920 if (minor >= ARRAY_SIZE(devlist))
923 dev = &devlist[minor];
927 filp->f_op = dev->fops;
929 filp->f_mapping->backing_dev_info = dev->dev_info;
932 ret = dev->fops->open(inode, filp);
940 static const struct file_operations memory_fops = {
944 static char *mem_devnode(struct device *dev, mode_t *mode)
946 if (mode && devlist[MINOR(dev->devt)].mode)
947 *mode = devlist[MINOR(dev->devt)].mode;
951 static struct class *mem_class;
953 static int __init chr_dev_init(void)
958 err = bdi_init(&zero_bdi);
962 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
963 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
965 mem_class = class_create(THIS_MODULE, "mem");
966 mem_class->devnode = mem_devnode;
967 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
968 if (!devlist[minor].name)
970 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
971 NULL, devlist[minor].name);
977 fs_initcall(chr_dev_init);