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>
39 * Architectures vary in how they handle caching for addresses
40 * outside of main memory.
43 static inline int uncached_access(struct file *file, unsigned long addr)
45 #if defined(CONFIG_IA64)
47 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
49 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
50 #elif defined(CONFIG_MIPS)
52 extern int __uncached_access(struct file *file,
55 return __uncached_access(file, addr);
59 * Accessing memory above the top the kernel knows about or through a file pointer
60 * that was marked O_SYNC will be done non-cached.
62 if (file->f_flags & O_SYNC)
64 return addr >= __pa(high_memory);
68 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
69 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
71 if (addr + count > __pa(high_memory))
77 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
83 #ifdef CONFIG_STRICT_DEVMEM
84 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
86 u64 from = ((u64)pfn) << PAGE_SHIFT;
91 if (!devmem_is_allowed(pfn)) {
93 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
94 current->comm, from, to);
103 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
109 void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
114 * This funcion reads the *physical* memory. The f_pos points directly to the
117 static ssize_t read_mem(struct file * file, char __user * buf,
118 size_t count, loff_t *ppos)
120 unsigned long p = *ppos;
124 if (!valid_phys_addr_range(p, count))
127 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
128 /* we don't have page 0 mapped on sparc and m68k.. */
134 if (clear_user(buf, sz))
146 * Handle first page in case it's not aligned
148 if (-p & (PAGE_SIZE - 1))
149 sz = -p & (PAGE_SIZE - 1);
153 sz = min_t(unsigned long, sz, count);
155 if (!range_is_allowed(p >> PAGE_SHIFT, count))
159 * On ia64 if a page has been mapped somewhere as
160 * uncached, then it must also be accessed uncached
161 * by the kernel or data corruption may occur
163 ptr = xlate_dev_mem_ptr(p);
167 if (copy_to_user(buf, ptr, sz)) {
168 unxlate_dev_mem_ptr(p, ptr);
172 unxlate_dev_mem_ptr(p, ptr);
184 static ssize_t write_mem(struct file * file, const char __user * buf,
185 size_t count, loff_t *ppos)
187 unsigned long p = *ppos;
189 unsigned long copied;
192 if (!valid_phys_addr_range(p, count))
197 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
198 /* we don't have page 0 mapped on sparc and m68k.. */
200 unsigned long sz = PAGE_SIZE - p;
203 /* Hmm. Do something? */
213 * Handle first page in case it's not aligned
215 if (-p & (PAGE_SIZE - 1))
216 sz = -p & (PAGE_SIZE - 1);
220 sz = min_t(unsigned long, sz, count);
222 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
226 * On ia64 if a page has been mapped somewhere as
227 * uncached, then it must also be accessed uncached
228 * by the kernel or data corruption may occur
230 ptr = xlate_dev_mem_ptr(p);
237 copied = copy_from_user(ptr, buf, sz);
239 written += sz - copied;
240 unxlate_dev_mem_ptr(p, ptr);
246 unxlate_dev_mem_ptr(p, ptr);
258 int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
259 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
264 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
265 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
266 unsigned long size, pgprot_t vma_prot)
268 #ifdef pgprot_noncached
269 unsigned long offset = pfn << PAGE_SHIFT;
271 if (uncached_access(file, offset))
272 return pgprot_noncached(vma_prot);
279 static unsigned long get_unmapped_area_mem(struct file *file,
285 if (!valid_mmap_phys_addr_range(pgoff, len))
286 return (unsigned long) -EINVAL;
287 return pgoff << PAGE_SHIFT;
290 /* can't do an in-place private mapping if there's no MMU */
291 static inline int private_mapping_ok(struct vm_area_struct *vma)
293 return vma->vm_flags & VM_MAYSHARE;
296 #define get_unmapped_area_mem NULL
298 static inline int private_mapping_ok(struct vm_area_struct *vma)
304 void __attribute__((weak))
305 map_devmem(unsigned long pfn, unsigned long len, pgprot_t prot)
307 /* nothing. architectures can override. */
310 void __attribute__((weak))
311 unmap_devmem(unsigned long pfn, unsigned long len, pgprot_t prot)
313 /* nothing. architectures can override. */
316 static void mmap_mem_open(struct vm_area_struct *vma)
318 map_devmem(vma->vm_pgoff, vma->vm_end - vma->vm_start,
322 static void mmap_mem_close(struct vm_area_struct *vma)
324 unmap_devmem(vma->vm_pgoff, vma->vm_end - vma->vm_start,
328 static struct vm_operations_struct mmap_mem_ops = {
329 .open = mmap_mem_open,
330 .close = mmap_mem_close,
331 #ifdef CONFIG_HAVE_IOREMAP_PROT
332 .access = generic_access_phys
336 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
338 size_t size = vma->vm_end - vma->vm_start;
340 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
343 if (!private_mapping_ok(vma))
346 if (!range_is_allowed(vma->vm_pgoff, size))
349 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
353 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
357 vma->vm_ops = &mmap_mem_ops;
359 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
360 if (remap_pfn_range(vma,
364 vma->vm_page_prot)) {
365 unmap_devmem(vma->vm_pgoff, size, vma->vm_page_prot);
371 #ifdef CONFIG_DEVKMEM
372 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
376 /* Turn a kernel-virtual address into a physical page frame */
377 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
380 * RED-PEN: on some architectures there is more mapped memory
381 * than available in mem_map which pfn_valid checks
382 * for. Perhaps should add a new macro here.
384 * RED-PEN: vmalloc is not supported right now.
390 return mmap_mem(file, vma);
394 #ifdef CONFIG_CRASH_DUMP
396 * Read memory corresponding to the old kernel.
398 static ssize_t read_oldmem(struct file *file, char __user *buf,
399 size_t count, loff_t *ppos)
401 unsigned long pfn, offset;
402 size_t read = 0, csize;
406 pfn = *ppos / PAGE_SIZE;
407 if (pfn > saved_max_pfn)
410 offset = (unsigned long)(*ppos % PAGE_SIZE);
411 if (count > PAGE_SIZE - offset)
412 csize = PAGE_SIZE - offset;
416 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
428 extern long vread(char *buf, char *addr, unsigned long count);
429 extern long vwrite(char *buf, char *addr, unsigned long count);
431 #ifdef CONFIG_DEVKMEM
433 * This function reads the *virtual* memory as seen by the kernel.
435 static ssize_t read_kmem(struct file *file, char __user *buf,
436 size_t count, loff_t *ppos)
438 unsigned long p = *ppos;
439 ssize_t low_count, read, sz;
440 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
443 if (p < (unsigned long) high_memory) {
445 if (count > (unsigned long) high_memory - p)
446 low_count = (unsigned long) high_memory - p;
448 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
449 /* we don't have page 0 mapped on sparc and m68k.. */
450 if (p < PAGE_SIZE && low_count > 0) {
451 size_t tmp = PAGE_SIZE - p;
452 if (tmp > low_count) tmp = low_count;
453 if (clear_user(buf, tmp))
462 while (low_count > 0) {
464 * Handle first page in case it's not aligned
466 if (-p & (PAGE_SIZE - 1))
467 sz = -p & (PAGE_SIZE - 1);
471 sz = min_t(unsigned long, sz, low_count);
474 * On ia64 if a page has been mapped somewhere as
475 * uncached, then it must also be accessed uncached
476 * by the kernel or data corruption may occur
478 kbuf = xlate_dev_kmem_ptr((char *)p);
480 if (copy_to_user(buf, kbuf, sz))
491 kbuf = (char *)__get_free_page(GFP_KERNEL);
499 len = vread(kbuf, (char *)p, len);
502 if (copy_to_user(buf, kbuf, len)) {
503 free_page((unsigned long)kbuf);
511 free_page((unsigned long)kbuf);
518 static inline ssize_t
519 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
520 size_t count, loff_t *ppos)
523 unsigned long copied;
526 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
527 /* we don't have page 0 mapped on sparc and m68k.. */
528 if (realp < PAGE_SIZE) {
529 unsigned long sz = PAGE_SIZE - realp;
532 /* Hmm. Do something? */
544 * Handle first page in case it's not aligned
546 if (-realp & (PAGE_SIZE - 1))
547 sz = -realp & (PAGE_SIZE - 1);
551 sz = min_t(unsigned long, sz, count);
554 * On ia64 if a page has been mapped somewhere as
555 * uncached, then it must also be accessed uncached
556 * by the kernel or data corruption may occur
558 ptr = xlate_dev_kmem_ptr(p);
560 copied = copy_from_user(ptr, buf, sz);
562 written += sz - copied;
580 * This function writes to the *virtual* memory as seen by the kernel.
582 static ssize_t write_kmem(struct file * file, const char __user * buf,
583 size_t count, loff_t *ppos)
585 unsigned long p = *ppos;
589 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
591 if (p < (unsigned long) high_memory) {
594 if (count > (unsigned long) high_memory - p)
595 wrote = (unsigned long) high_memory - p;
597 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
598 if (written != wrote)
607 kbuf = (char *)__get_free_page(GFP_KERNEL);
609 return wrote ? wrote : -ENOMEM;
616 written = copy_from_user(kbuf, buf, len);
620 free_page((unsigned long)kbuf);
624 len = vwrite(kbuf, (char *)p, len);
630 free_page((unsigned long)kbuf);
634 return virtr + wrote;
638 #ifdef CONFIG_DEVPORT
639 static ssize_t read_port(struct file * file, char __user * buf,
640 size_t count, loff_t *ppos)
642 unsigned long i = *ppos;
643 char __user *tmp = buf;
645 if (!access_ok(VERIFY_WRITE, buf, count))
647 while (count-- > 0 && i < 65536) {
648 if (__put_user(inb(i),tmp) < 0)
657 static ssize_t write_port(struct file * file, const char __user * buf,
658 size_t count, loff_t *ppos)
660 unsigned long i = *ppos;
661 const char __user * tmp = buf;
663 if (!access_ok(VERIFY_READ,buf,count))
665 while (count-- > 0 && i < 65536) {
667 if (__get_user(c, tmp)) {
681 static ssize_t read_null(struct file * file, char __user * buf,
682 size_t count, loff_t *ppos)
687 static ssize_t write_null(struct file * file, const char __user * buf,
688 size_t count, loff_t *ppos)
693 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
694 struct splice_desc *sd)
699 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
700 loff_t *ppos, size_t len, unsigned int flags)
702 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
705 static ssize_t read_zero(struct file * file, char __user * buf,
706 size_t count, loff_t *ppos)
713 if (!access_ok(VERIFY_WRITE, buf, count))
718 unsigned long unwritten;
719 size_t chunk = count;
721 if (chunk > PAGE_SIZE)
722 chunk = PAGE_SIZE; /* Just for latency reasons */
723 unwritten = clear_user(buf, chunk);
724 written += chunk - unwritten;
731 return written ? written : -EFAULT;
734 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
739 if (vma->vm_flags & VM_SHARED)
740 return shmem_zero_setup(vma);
744 static ssize_t write_full(struct file * file, const char __user * buf,
745 size_t count, loff_t *ppos)
751 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
752 * can fopen() both devices with "a" now. This was previously impossible.
756 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
758 return file->f_pos = 0;
762 * The memory devices use the full 32/64 bits of the offset, and so we cannot
763 * check against negative addresses: they are ok. The return value is weird,
764 * though, in that case (0).
766 * also note that seeking relative to the "end of file" isn't supported:
767 * it has no meaning, so it returns -EINVAL.
769 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
773 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
776 file->f_pos = offset;
778 force_successful_syscall_return();
781 file->f_pos += offset;
783 force_successful_syscall_return();
788 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
792 static int open_port(struct inode * inode, struct file * filp)
794 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
797 #define zero_lseek null_lseek
798 #define full_lseek null_lseek
799 #define write_zero write_null
800 #define read_full read_zero
801 #define open_mem open_port
802 #define open_kmem open_mem
803 #define open_oldmem open_mem
805 static const struct file_operations mem_fops = {
806 .llseek = memory_lseek,
811 .get_unmapped_area = get_unmapped_area_mem,
814 #ifdef CONFIG_DEVKMEM
815 static const struct file_operations kmem_fops = {
816 .llseek = memory_lseek,
821 .get_unmapped_area = get_unmapped_area_mem,
825 static const struct file_operations null_fops = {
826 .llseek = null_lseek,
829 .splice_write = splice_write_null,
832 #ifdef CONFIG_DEVPORT
833 static const struct file_operations port_fops = {
834 .llseek = memory_lseek,
841 static const struct file_operations zero_fops = {
842 .llseek = zero_lseek,
849 * capabilities for /dev/zero
850 * - permits private mappings, "copies" are taken of the source of zeros
852 static struct backing_dev_info zero_bdi = {
853 .capabilities = BDI_CAP_MAP_COPY,
856 static const struct file_operations full_fops = {
857 .llseek = full_lseek,
862 #ifdef CONFIG_CRASH_DUMP
863 static const struct file_operations oldmem_fops = {
869 static ssize_t kmsg_write(struct file * file, const char __user * buf,
870 size_t count, loff_t *ppos)
875 tmp = kmalloc(count + 1, GFP_KERNEL);
879 if (!copy_from_user(tmp, buf, count)) {
881 ret = printk("%s", tmp);
883 /* printk can add a prefix */
890 static const struct file_operations kmsg_fops = {
894 static int memory_open(struct inode * inode, struct file * filp)
899 switch (iminor(inode)) {
901 filp->f_op = &mem_fops;
902 filp->f_mapping->backing_dev_info =
903 &directly_mappable_cdev_bdi;
905 #ifdef CONFIG_DEVKMEM
907 filp->f_op = &kmem_fops;
908 filp->f_mapping->backing_dev_info =
909 &directly_mappable_cdev_bdi;
913 filp->f_op = &null_fops;
915 #ifdef CONFIG_DEVPORT
917 filp->f_op = &port_fops;
921 filp->f_mapping->backing_dev_info = &zero_bdi;
922 filp->f_op = &zero_fops;
925 filp->f_op = &full_fops;
928 filp->f_op = &random_fops;
931 filp->f_op = &urandom_fops;
934 filp->f_op = &kmsg_fops;
936 #ifdef CONFIG_CRASH_DUMP
938 filp->f_op = &oldmem_fops;
945 if (filp->f_op && filp->f_op->open)
946 ret = filp->f_op->open(inode,filp);
951 static const struct file_operations memory_fops = {
952 .open = memory_open, /* just a selector for the real open */
955 static const struct {
959 const struct file_operations *fops;
960 } devlist[] = { /* list of minor devices */
961 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
962 #ifdef CONFIG_DEVKMEM
963 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
965 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
966 #ifdef CONFIG_DEVPORT
967 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
969 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
970 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
971 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
972 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
973 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
974 #ifdef CONFIG_CRASH_DUMP
975 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
979 static struct class *mem_class;
981 static int __init chr_dev_init(void)
986 err = bdi_init(&zero_bdi);
990 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
991 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
993 mem_class = class_create(THIS_MODULE, "mem");
994 for (i = 0; i < ARRAY_SIZE(devlist); i++)
995 device_create(mem_class, NULL,
996 MKDEV(MEM_MAJOR, devlist[i].minor), NULL,
1002 fs_initcall(chr_dev_init);