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>
30 #include <asm/uaccess.h>
34 # include <linux/efi.h>
38 * Architectures vary in how they handle caching for addresses
39 * outside of main memory.
42 static inline int uncached_access(struct file *file, unsigned long addr)
44 #if defined(__i386__) && !defined(__arch_um__)
46 * On the PPro and successors, the MTRRs are used to set
47 * memory types for physical addresses outside main memory,
48 * so blindly setting PCD or PWT on those pages is wrong.
49 * For Pentiums and earlier, the surround logic should disable
50 * caching for the high addresses through the KEN pin, but
51 * we maintain the tradition of paranoia in this code.
53 if (file->f_flags & O_SYNC)
55 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
56 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
57 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
58 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
59 && addr >= __pa(high_memory);
60 #elif defined(__x86_64__) && !defined(__arch_um__)
62 * This is broken because it can generate memory type aliases,
63 * which can cause cache corruptions
64 * But it is only available for root and we have to be bug-to-bug
65 * compatible with i386.
67 if (file->f_flags & O_SYNC)
69 /* same behaviour as i386. PAT always set to cached and MTRRs control the
71 Hopefully a full PAT implementation will fix that soon. */
73 #elif defined(CONFIG_IA64)
75 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
77 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
78 #elif defined(CONFIG_MIPS)
80 extern int __uncached_access(struct file *file,
83 return __uncached_access(file, addr);
87 * Accessing memory above the top the kernel knows about or through a file pointer
88 * that was marked O_SYNC will be done non-cached.
90 if (file->f_flags & O_SYNC)
92 return addr >= __pa(high_memory);
96 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
97 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
99 if (addr + count > __pa(high_memory))
105 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
111 #ifdef CONFIG_NONPROMISC_DEVMEM
112 static inline int range_is_allowed(unsigned long from, unsigned long to)
114 unsigned long cursor;
116 cursor = from >> PAGE_SHIFT;
117 while ((cursor << PAGE_SHIFT) < to) {
118 if (!devmem_is_allowed(cursor)) {
119 printk(KERN_INFO "Program %s tried to read /dev/mem "
120 "between %lx->%lx.\n",
121 current->comm, from, to);
129 static inline int range_is_allowed(unsigned long from, unsigned long to)
136 * This funcion reads the *physical* memory. The f_pos points directly to the
139 static ssize_t read_mem(struct file * file, char __user * buf,
140 size_t count, loff_t *ppos)
142 unsigned long p = *ppos;
146 if (!valid_phys_addr_range(p, count))
149 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
150 /* we don't have page 0 mapped on sparc and m68k.. */
156 if (clear_user(buf, sz))
168 * Handle first page in case it's not aligned
170 if (-p & (PAGE_SIZE - 1))
171 sz = -p & (PAGE_SIZE - 1);
175 sz = min_t(unsigned long, sz, count);
178 * On ia64 if a page has been mapped somewhere as
179 * uncached, then it must also be accessed uncached
180 * by the kernel or data corruption may occur
182 ptr = xlate_dev_mem_ptr(p);
184 if (!range_is_allowed(p, p+count))
186 if (copy_to_user(buf, ptr, sz))
198 static ssize_t write_mem(struct file * file, const char __user * buf,
199 size_t count, loff_t *ppos)
201 unsigned long p = *ppos;
203 unsigned long copied;
206 if (!valid_phys_addr_range(p, count))
211 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
212 /* we don't have page 0 mapped on sparc and m68k.. */
214 unsigned long sz = PAGE_SIZE - p;
217 /* Hmm. Do something? */
227 * Handle first page in case it's not aligned
229 if (-p & (PAGE_SIZE - 1))
230 sz = -p & (PAGE_SIZE - 1);
234 sz = min_t(unsigned long, sz, count);
237 * On ia64 if a page has been mapped somewhere as
238 * uncached, then it must also be accessed uncached
239 * by the kernel or data corruption may occur
241 ptr = xlate_dev_mem_ptr(p);
243 if (!range_is_allowed(p, p+sz))
245 copied = copy_from_user(ptr, buf, sz);
247 written += sz - copied;
262 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
263 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
264 unsigned long size, pgprot_t vma_prot)
266 #ifdef pgprot_noncached
267 unsigned long offset = pfn << PAGE_SHIFT;
269 if (uncached_access(file, offset))
270 return pgprot_noncached(vma_prot);
277 static unsigned long get_unmapped_area_mem(struct file *file,
283 if (!valid_mmap_phys_addr_range(pgoff, len))
284 return (unsigned long) -EINVAL;
285 return pgoff << PAGE_SHIFT;
288 /* can't do an in-place private mapping if there's no MMU */
289 static inline int private_mapping_ok(struct vm_area_struct *vma)
291 return vma->vm_flags & VM_MAYSHARE;
294 #define get_unmapped_area_mem NULL
296 static inline int private_mapping_ok(struct vm_area_struct *vma)
302 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
304 size_t size = vma->vm_end - vma->vm_start;
306 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
309 if (!private_mapping_ok(vma))
312 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
316 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
317 if (remap_pfn_range(vma,
326 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
330 /* Turn a kernel-virtual address into a physical page frame */
331 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
334 * RED-PEN: on some architectures there is more mapped memory
335 * than available in mem_map which pfn_valid checks
336 * for. Perhaps should add a new macro here.
338 * RED-PEN: vmalloc is not supported right now.
344 return mmap_mem(file, vma);
347 #ifdef CONFIG_CRASH_DUMP
349 * Read memory corresponding to the old kernel.
351 static ssize_t read_oldmem(struct file *file, char __user *buf,
352 size_t count, loff_t *ppos)
354 unsigned long pfn, offset;
355 size_t read = 0, csize;
359 pfn = *ppos / PAGE_SIZE;
360 if (pfn > saved_max_pfn)
363 offset = (unsigned long)(*ppos % PAGE_SIZE);
364 if (count > PAGE_SIZE - offset)
365 csize = PAGE_SIZE - offset;
369 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
381 extern long vread(char *buf, char *addr, unsigned long count);
382 extern long vwrite(char *buf, char *addr, unsigned long count);
385 * This function reads the *virtual* memory as seen by the kernel.
387 static ssize_t read_kmem(struct file *file, char __user *buf,
388 size_t count, loff_t *ppos)
390 unsigned long p = *ppos;
391 ssize_t low_count, read, sz;
392 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
395 if (p < (unsigned long) high_memory) {
397 if (count > (unsigned long) high_memory - p)
398 low_count = (unsigned long) high_memory - p;
400 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
401 /* we don't have page 0 mapped on sparc and m68k.. */
402 if (p < PAGE_SIZE && low_count > 0) {
403 size_t tmp = PAGE_SIZE - p;
404 if (tmp > low_count) tmp = low_count;
405 if (clear_user(buf, tmp))
414 while (low_count > 0) {
416 * Handle first page in case it's not aligned
418 if (-p & (PAGE_SIZE - 1))
419 sz = -p & (PAGE_SIZE - 1);
423 sz = min_t(unsigned long, sz, low_count);
426 * On ia64 if a page has been mapped somewhere as
427 * uncached, then it must also be accessed uncached
428 * by the kernel or data corruption may occur
430 kbuf = xlate_dev_kmem_ptr((char *)p);
432 if (copy_to_user(buf, kbuf, sz))
443 kbuf = (char *)__get_free_page(GFP_KERNEL);
451 len = vread(kbuf, (char *)p, len);
454 if (copy_to_user(buf, kbuf, len)) {
455 free_page((unsigned long)kbuf);
463 free_page((unsigned long)kbuf);
470 static inline ssize_t
471 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
472 size_t count, loff_t *ppos)
475 unsigned long copied;
478 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
479 /* we don't have page 0 mapped on sparc and m68k.. */
480 if (realp < PAGE_SIZE) {
481 unsigned long sz = PAGE_SIZE - realp;
484 /* Hmm. Do something? */
496 * Handle first page in case it's not aligned
498 if (-realp & (PAGE_SIZE - 1))
499 sz = -realp & (PAGE_SIZE - 1);
503 sz = min_t(unsigned long, sz, count);
506 * On ia64 if a page has been mapped somewhere as
507 * uncached, then it must also be accessed uncached
508 * by the kernel or data corruption may occur
510 ptr = xlate_dev_kmem_ptr(p);
512 copied = copy_from_user(ptr, buf, sz);
514 written += sz - copied;
532 * This function writes to the *virtual* memory as seen by the kernel.
534 static ssize_t write_kmem(struct file * file, const char __user * buf,
535 size_t count, loff_t *ppos)
537 unsigned long p = *ppos;
541 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
543 if (p < (unsigned long) high_memory) {
546 if (count > (unsigned long) high_memory - p)
547 wrote = (unsigned long) high_memory - p;
549 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
550 if (written != wrote)
559 kbuf = (char *)__get_free_page(GFP_KERNEL);
561 return wrote ? wrote : -ENOMEM;
568 written = copy_from_user(kbuf, buf, len);
572 free_page((unsigned long)kbuf);
576 len = vwrite(kbuf, (char *)p, len);
582 free_page((unsigned long)kbuf);
586 return virtr + wrote;
589 #ifdef CONFIG_DEVPORT
590 static ssize_t read_port(struct file * file, char __user * buf,
591 size_t count, loff_t *ppos)
593 unsigned long i = *ppos;
594 char __user *tmp = buf;
596 if (!access_ok(VERIFY_WRITE, buf, count))
598 while (count-- > 0 && i < 65536) {
599 if (__put_user(inb(i),tmp) < 0)
608 static ssize_t write_port(struct file * file, const char __user * buf,
609 size_t count, loff_t *ppos)
611 unsigned long i = *ppos;
612 const char __user * tmp = buf;
614 if (!access_ok(VERIFY_READ,buf,count))
616 while (count-- > 0 && i < 65536) {
618 if (__get_user(c, tmp)) {
632 static ssize_t read_null(struct file * file, char __user * buf,
633 size_t count, loff_t *ppos)
638 static ssize_t write_null(struct file * file, const char __user * buf,
639 size_t count, loff_t *ppos)
644 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
645 struct splice_desc *sd)
650 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
651 loff_t *ppos, size_t len, unsigned int flags)
653 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
656 static ssize_t read_zero(struct file * file, char __user * buf,
657 size_t count, loff_t *ppos)
664 if (!access_ok(VERIFY_WRITE, buf, count))
669 unsigned long unwritten;
670 size_t chunk = count;
672 if (chunk > PAGE_SIZE)
673 chunk = PAGE_SIZE; /* Just for latency reasons */
674 unwritten = clear_user(buf, chunk);
675 written += chunk - unwritten;
682 return written ? written : -EFAULT;
685 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
690 if (vma->vm_flags & VM_SHARED)
691 return shmem_zero_setup(vma);
695 static ssize_t write_full(struct file * file, const char __user * buf,
696 size_t count, loff_t *ppos)
702 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
703 * can fopen() both devices with "a" now. This was previously impossible.
707 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
709 return file->f_pos = 0;
713 * The memory devices use the full 32/64 bits of the offset, and so we cannot
714 * check against negative addresses: they are ok. The return value is weird,
715 * though, in that case (0).
717 * also note that seeking relative to the "end of file" isn't supported:
718 * it has no meaning, so it returns -EINVAL.
720 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
724 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
727 file->f_pos = offset;
729 force_successful_syscall_return();
732 file->f_pos += offset;
734 force_successful_syscall_return();
739 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
743 static int open_port(struct inode * inode, struct file * filp)
745 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
748 #define zero_lseek null_lseek
749 #define full_lseek null_lseek
750 #define write_zero write_null
751 #define read_full read_zero
752 #define open_mem open_port
753 #define open_kmem open_mem
754 #define open_oldmem open_mem
756 static const struct file_operations mem_fops = {
757 .llseek = memory_lseek,
762 .get_unmapped_area = get_unmapped_area_mem,
765 static const struct file_operations kmem_fops = {
766 .llseek = memory_lseek,
771 .get_unmapped_area = get_unmapped_area_mem,
774 static const struct file_operations null_fops = {
775 .llseek = null_lseek,
778 .splice_write = splice_write_null,
781 #ifdef CONFIG_DEVPORT
782 static const struct file_operations port_fops = {
783 .llseek = memory_lseek,
790 static const struct file_operations zero_fops = {
791 .llseek = zero_lseek,
798 * capabilities for /dev/zero
799 * - permits private mappings, "copies" are taken of the source of zeros
801 static struct backing_dev_info zero_bdi = {
802 .capabilities = BDI_CAP_MAP_COPY,
805 static const struct file_operations full_fops = {
806 .llseek = full_lseek,
811 #ifdef CONFIG_CRASH_DUMP
812 static const struct file_operations oldmem_fops = {
818 static ssize_t kmsg_write(struct file * file, const char __user * buf,
819 size_t count, loff_t *ppos)
824 tmp = kmalloc(count + 1, GFP_KERNEL);
828 if (!copy_from_user(tmp, buf, count)) {
830 ret = printk("%s", tmp);
832 /* printk can add a prefix */
839 static const struct file_operations kmsg_fops = {
843 static int memory_open(struct inode * inode, struct file * filp)
845 switch (iminor(inode)) {
847 filp->f_op = &mem_fops;
848 filp->f_mapping->backing_dev_info =
849 &directly_mappable_cdev_bdi;
852 filp->f_op = &kmem_fops;
853 filp->f_mapping->backing_dev_info =
854 &directly_mappable_cdev_bdi;
857 filp->f_op = &null_fops;
859 #ifdef CONFIG_DEVPORT
861 filp->f_op = &port_fops;
865 filp->f_mapping->backing_dev_info = &zero_bdi;
866 filp->f_op = &zero_fops;
869 filp->f_op = &full_fops;
872 filp->f_op = &random_fops;
875 filp->f_op = &urandom_fops;
878 filp->f_op = &kmsg_fops;
880 #ifdef CONFIG_CRASH_DUMP
882 filp->f_op = &oldmem_fops;
888 if (filp->f_op && filp->f_op->open)
889 return filp->f_op->open(inode,filp);
893 static const struct file_operations memory_fops = {
894 .open = memory_open, /* just a selector for the real open */
897 static const struct {
901 const struct file_operations *fops;
902 } devlist[] = { /* list of minor devices */
903 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
904 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
905 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
906 #ifdef CONFIG_DEVPORT
907 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
909 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
910 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
911 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
912 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
913 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
914 #ifdef CONFIG_CRASH_DUMP
915 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
919 static struct class *mem_class;
921 static int __init chr_dev_init(void)
926 err = bdi_init(&zero_bdi);
930 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
931 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
933 mem_class = class_create(THIS_MODULE, "mem");
934 for (i = 0; i < ARRAY_SIZE(devlist); i++)
935 device_create(mem_class, NULL,
936 MKDEV(MEM_MAJOR, devlist[i].minor),
942 fs_initcall(chr_dev_init);