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>
11 #include <linux/config.h>
13 #include <linux/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mman.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/raw.h>
20 #include <linux/tty.h>
21 #include <linux/capability.h>
22 #include <linux/smp_lock.h>
23 #include <linux/devfs_fs_kernel.h>
24 #include <linux/ptrace.h>
25 #include <linux/device.h>
26 #include <linux/highmem.h>
27 #include <linux/crash_dump.h>
28 #include <linux/backing-dev.h>
29 #include <linux/bootmem.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)
47 * On the PPro and successors, the MTRRs are used to set
48 * memory types for physical addresses outside main memory,
49 * so blindly setting PCD or PWT on those pages is wrong.
50 * For Pentiums and earlier, the surround logic should disable
51 * caching for the high addresses through the KEN pin, but
52 * we maintain the tradition of paranoia in this code.
54 if (file->f_flags & O_SYNC)
56 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
57 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
58 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
59 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
60 && addr >= __pa(high_memory);
61 #elif defined(__x86_64__)
63 * This is broken because it can generate memory type aliases,
64 * which can cause cache corruptions
65 * But it is only available for root and we have to be bug-to-bug
66 * compatible with i386.
68 if (file->f_flags & O_SYNC)
70 /* same behaviour as i386. PAT always set to cached and MTRRs control the
72 Hopefully a full PAT implementation will fix that soon. */
74 #elif defined(CONFIG_IA64)
76 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
78 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
81 * Accessing memory above the top the kernel knows about or through a file pointer
82 * that was marked O_SYNC will be done non-cached.
84 if (file->f_flags & O_SYNC)
86 return addr >= __pa(high_memory);
90 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
91 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
93 unsigned long end_mem;
95 end_mem = __pa(high_memory);
99 if (*count > end_mem - addr)
100 *count = end_mem - addr;
107 * This funcion reads the *physical* memory. The f_pos points directly to the
110 static ssize_t read_mem(struct file * file, char __user * buf,
111 size_t count, loff_t *ppos)
113 unsigned long p = *ppos;
117 if (!valid_phys_addr_range(p, &count))
120 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
121 /* we don't have page 0 mapped on sparc and m68k.. */
127 if (clear_user(buf, sz))
139 * Handle first page in case it's not aligned
141 if (-p & (PAGE_SIZE - 1))
142 sz = -p & (PAGE_SIZE - 1);
146 sz = min_t(unsigned long, sz, count);
149 * On ia64 if a page has been mapped somewhere as
150 * uncached, then it must also be accessed uncached
151 * by the kernel or data corruption may occur
153 ptr = xlate_dev_mem_ptr(p);
155 if (copy_to_user(buf, ptr, sz))
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 unsigned long sz = PAGE_SIZE - p;
186 /* Hmm. Do something? */
196 * Handle first page in case it's not aligned
198 if (-p & (PAGE_SIZE - 1))
199 sz = -p & (PAGE_SIZE - 1);
203 sz = min_t(unsigned long, sz, count);
206 * On ia64 if a page has been mapped somewhere as
207 * uncached, then it must also be accessed uncached
208 * by the kernel or data corruption may occur
210 ptr = xlate_dev_mem_ptr(p);
212 copied = copy_from_user(ptr, buf, sz);
216 ret = written + (sz - copied);
231 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
233 #if defined(__HAVE_PHYS_MEM_ACCESS_PROT)
234 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
235 vma->vm_end - vma->vm_start,
237 #elif defined(pgprot_noncached)
238 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
241 uncached = uncached_access(file, offset);
243 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
246 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
247 if (remap_pfn_range(vma,
250 vma->vm_end-vma->vm_start,
256 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
260 /* Turn a kernel-virtual address into a physical page frame */
261 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
264 * RED-PEN: on some architectures there is more mapped memory
265 * than available in mem_map which pfn_valid checks
266 * for. Perhaps should add a new macro here.
268 * RED-PEN: vmalloc is not supported right now.
274 return mmap_mem(file, vma);
277 #ifdef CONFIG_CRASH_DUMP
279 * Read memory corresponding to the old kernel.
281 static ssize_t read_oldmem(struct file *file, char __user *buf,
282 size_t count, loff_t *ppos)
284 unsigned long pfn, offset;
285 size_t read = 0, csize;
289 pfn = *ppos / PAGE_SIZE;
290 if (pfn > saved_max_pfn)
293 offset = (unsigned long)(*ppos % PAGE_SIZE);
294 if (count > PAGE_SIZE - offset)
295 csize = PAGE_SIZE - offset;
299 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
311 extern long vread(char *buf, char *addr, unsigned long count);
312 extern long vwrite(char *buf, char *addr, unsigned long count);
315 * This function reads the *virtual* memory as seen by the kernel.
317 static ssize_t read_kmem(struct file *file, char __user *buf,
318 size_t count, loff_t *ppos)
320 unsigned long p = *ppos;
321 ssize_t low_count, read, sz;
322 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
325 if (p < (unsigned long) high_memory) {
327 if (count > (unsigned long) high_memory - p)
328 low_count = (unsigned long) high_memory - p;
330 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
331 /* we don't have page 0 mapped on sparc and m68k.. */
332 if (p < PAGE_SIZE && low_count > 0) {
333 size_t tmp = PAGE_SIZE - p;
334 if (tmp > low_count) tmp = low_count;
335 if (clear_user(buf, tmp))
344 while (low_count > 0) {
346 * Handle first page in case it's not aligned
348 if (-p & (PAGE_SIZE - 1))
349 sz = -p & (PAGE_SIZE - 1);
353 sz = min_t(unsigned long, sz, low_count);
356 * On ia64 if a page has been mapped somewhere as
357 * uncached, then it must also be accessed uncached
358 * by the kernel or data corruption may occur
360 kbuf = xlate_dev_kmem_ptr((char *)p);
362 if (copy_to_user(buf, kbuf, sz))
373 kbuf = (char *)__get_free_page(GFP_KERNEL);
381 len = vread(kbuf, (char *)p, len);
384 if (copy_to_user(buf, kbuf, len)) {
385 free_page((unsigned long)kbuf);
393 free_page((unsigned long)kbuf);
400 static inline ssize_t
401 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
402 size_t count, loff_t *ppos)
405 unsigned long copied;
408 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
409 /* we don't have page 0 mapped on sparc and m68k.. */
410 if (realp < PAGE_SIZE) {
411 unsigned long sz = PAGE_SIZE - realp;
414 /* Hmm. Do something? */
426 * Handle first page in case it's not aligned
428 if (-realp & (PAGE_SIZE - 1))
429 sz = -realp & (PAGE_SIZE - 1);
433 sz = min_t(unsigned long, sz, 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 ptr = xlate_dev_kmem_ptr(p);
442 copied = copy_from_user(ptr, buf, sz);
446 ret = written + (sz - copied);
464 * This function writes to the *virtual* memory as seen by the kernel.
466 static ssize_t write_kmem(struct file * file, const char __user * buf,
467 size_t count, loff_t *ppos)
469 unsigned long p = *ppos;
473 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
475 if (p < (unsigned long) high_memory) {
478 if (count > (unsigned long) high_memory - p)
479 wrote = (unsigned long) high_memory - p;
481 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
482 if (written != wrote)
491 kbuf = (char *)__get_free_page(GFP_KERNEL);
493 return wrote ? wrote : -ENOMEM;
500 written = copy_from_user(kbuf, buf, len);
504 free_page((unsigned long)kbuf);
505 ret = wrote + virtr + (len - written);
506 return ret ? ret : -EFAULT;
509 len = vwrite(kbuf, (char *)p, len);
515 free_page((unsigned long)kbuf);
519 return virtr + wrote;
522 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
523 static ssize_t read_port(struct file * file, char __user * buf,
524 size_t count, loff_t *ppos)
526 unsigned long i = *ppos;
527 char __user *tmp = buf;
529 if (!access_ok(VERIFY_WRITE, buf, count))
531 while (count-- > 0 && i < 65536) {
532 if (__put_user(inb(i),tmp) < 0)
541 static ssize_t write_port(struct file * file, const char __user * buf,
542 size_t count, loff_t *ppos)
544 unsigned long i = *ppos;
545 const char __user * tmp = buf;
547 if (!access_ok(VERIFY_READ,buf,count))
549 while (count-- > 0 && i < 65536) {
551 if (__get_user(c, tmp))
562 static ssize_t read_null(struct file * file, char __user * buf,
563 size_t count, loff_t *ppos)
568 static ssize_t write_null(struct file * file, const char __user * buf,
569 size_t count, loff_t *ppos)
576 * For fun, we are using the MMU for this.
578 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
580 struct mm_struct *mm;
581 struct vm_area_struct * vma;
582 unsigned long addr=(unsigned long)buf;
585 /* Oops, this was forgotten before. -ben */
586 down_read(&mm->mmap_sem);
588 /* For private mappings, just map in zero pages. */
589 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
592 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
594 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
596 count = vma->vm_end - addr;
600 zap_page_range(vma, addr, count, NULL);
601 zeromap_page_range(vma, addr, count, PAGE_COPY);
610 up_read(&mm->mmap_sem);
612 /* The shared case is hard. Let's do the conventional zeroing. */
614 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
616 return size + unwritten - PAGE_SIZE;
624 up_read(&mm->mmap_sem);
628 static ssize_t read_zero(struct file * file, char __user * buf,
629 size_t count, loff_t *ppos)
631 unsigned long left, unwritten, written = 0;
636 if (!access_ok(VERIFY_WRITE, buf, count))
641 /* do we want to be clever? Arbitrary cut-off */
642 if (count >= PAGE_SIZE*4) {
643 unsigned long partial;
645 /* How much left of the page? */
646 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
647 unwritten = clear_user(buf, partial);
648 written = partial - unwritten;
653 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
654 written += (left & PAGE_MASK) - unwritten;
657 buf += left & PAGE_MASK;
660 unwritten = clear_user(buf, left);
661 written += left - unwritten;
663 return written ? written : -EFAULT;
666 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
668 if (vma->vm_flags & VM_SHARED)
669 return shmem_zero_setup(vma);
670 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
674 #else /* CONFIG_MMU */
675 static ssize_t read_zero(struct file * file, char * buf,
676 size_t count, loff_t *ppos)
684 chunk = 4096; /* Just for latency reasons */
685 if (clear_user(buf, chunk))
694 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
698 #endif /* CONFIG_MMU */
700 static ssize_t write_full(struct file * file, const char __user * buf,
701 size_t count, loff_t *ppos)
707 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
708 * can fopen() both devices with "a" now. This was previously impossible.
712 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
714 return file->f_pos = 0;
718 * The memory devices use the full 32/64 bits of the offset, and so we cannot
719 * check against negative addresses: they are ok. The return value is weird,
720 * though, in that case (0).
722 * also note that seeking relative to the "end of file" isn't supported:
723 * it has no meaning, so it returns -EINVAL.
725 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
729 down(&file->f_dentry->d_inode->i_sem);
732 file->f_pos = offset;
734 force_successful_syscall_return();
737 file->f_pos += offset;
739 force_successful_syscall_return();
744 up(&file->f_dentry->d_inode->i_sem);
748 static int open_port(struct inode * inode, struct file * filp)
750 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
753 #define zero_lseek null_lseek
754 #define full_lseek null_lseek
755 #define write_zero write_null
756 #define read_full read_zero
757 #define open_mem open_port
758 #define open_kmem open_mem
759 #define open_oldmem open_mem
761 static struct file_operations mem_fops = {
762 .llseek = memory_lseek,
769 static struct file_operations kmem_fops = {
770 .llseek = memory_lseek,
777 static struct file_operations null_fops = {
778 .llseek = null_lseek,
783 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
784 static struct file_operations port_fops = {
785 .llseek = memory_lseek,
792 static struct file_operations zero_fops = {
793 .llseek = zero_lseek,
799 static struct backing_dev_info zero_bdi = {
800 .capabilities = BDI_CAP_MAP_COPY,
803 static struct file_operations full_fops = {
804 .llseek = full_lseek,
809 #ifdef CONFIG_CRASH_DUMP
810 static struct file_operations oldmem_fops = {
816 static ssize_t kmsg_write(struct file * file, const char __user * buf,
817 size_t count, loff_t *ppos)
822 tmp = kmalloc(count + 1, GFP_KERNEL);
826 if (!copy_from_user(tmp, buf, count)) {
828 ret = printk("%s", tmp);
834 static struct file_operations kmsg_fops = {
838 static int memory_open(struct inode * inode, struct file * filp)
840 switch (iminor(inode)) {
842 filp->f_op = &mem_fops;
845 filp->f_op = &kmem_fops;
848 filp->f_op = &null_fops;
850 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
852 filp->f_op = &port_fops;
856 filp->f_mapping->backing_dev_info = &zero_bdi;
857 filp->f_op = &zero_fops;
860 filp->f_op = &full_fops;
863 filp->f_op = &random_fops;
866 filp->f_op = &urandom_fops;
869 filp->f_op = &kmsg_fops;
871 #ifdef CONFIG_CRASH_DUMP
873 filp->f_op = &oldmem_fops;
879 if (filp->f_op && filp->f_op->open)
880 return filp->f_op->open(inode,filp);
884 static struct file_operations memory_fops = {
885 .open = memory_open, /* just a selector for the real open */
888 static const struct {
892 struct file_operations *fops;
893 } devlist[] = { /* list of minor devices */
894 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
895 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
896 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
897 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
898 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
900 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
901 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
902 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
903 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
904 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
905 #ifdef CONFIG_CRASH_DUMP
906 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
910 static struct class *mem_class;
912 static int __init chr_dev_init(void)
916 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
917 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
919 mem_class = class_create(THIS_MODULE, "mem");
920 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
921 class_device_create(mem_class, NULL,
922 MKDEV(MEM_MAJOR, devlist[i].minor),
923 NULL, devlist[i].name);
924 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
925 S_IFCHR | devlist[i].mode, devlist[i].name);
931 fs_initcall(chr_dev_init);