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>
38 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
39 extern void tapechar_init(void);
43 * Architectures vary in how they handle caching for addresses
44 * outside of main memory.
47 static inline int uncached_access(struct file *file, unsigned long addr)
51 * On the PPro and successors, the MTRRs are used to set
52 * memory types for physical addresses outside main memory,
53 * so blindly setting PCD or PWT on those pages is wrong.
54 * For Pentiums and earlier, the surround logic should disable
55 * caching for the high addresses through the KEN pin, but
56 * we maintain the tradition of paranoia in this code.
58 if (file->f_flags & O_SYNC)
60 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
61 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
62 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
63 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
64 && addr >= __pa(high_memory);
65 #elif defined(__x86_64__)
67 * This is broken because it can generate memory type aliases,
68 * which can cause cache corruptions
69 * But it is only available for root and we have to be bug-to-bug
70 * compatible with i386.
72 if (file->f_flags & O_SYNC)
74 /* same behaviour as i386. PAT always set to cached and MTRRs control the
76 Hopefully a full PAT implementation will fix that soon. */
78 #elif defined(CONFIG_IA64)
80 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
82 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
85 * Accessing memory above the top the kernel knows about or through a file pointer
86 * that was marked O_SYNC will be done non-cached.
88 if (file->f_flags & O_SYNC)
90 return addr >= __pa(high_memory);
94 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
95 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
97 unsigned long end_mem;
99 end_mem = __pa(high_memory);
103 if (*count > end_mem - addr)
104 *count = end_mem - addr;
111 * This funcion reads the *physical* memory. The f_pos points directly to the
114 static ssize_t read_mem(struct file * file, char __user * buf,
115 size_t count, loff_t *ppos)
117 unsigned long p = *ppos;
121 if (!valid_phys_addr_range(p, &count))
124 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
125 /* we don't have page 0 mapped on sparc and m68k.. */
131 if (clear_user(buf, sz))
143 * Handle first page in case it's not aligned
145 if (-p & (PAGE_SIZE - 1))
146 sz = -p & (PAGE_SIZE - 1);
150 sz = min_t(unsigned long, sz, count);
153 * On ia64 if a page has been mapped somewhere as
154 * uncached, then it must also be accessed uncached
155 * by the kernel or data corruption may occur
157 ptr = xlate_dev_mem_ptr(p);
159 if (copy_to_user(buf, ptr, sz))
171 static ssize_t write_mem(struct file * file, const char __user * buf,
172 size_t count, loff_t *ppos)
174 unsigned long p = *ppos;
176 unsigned long copied;
179 if (!valid_phys_addr_range(p, &count))
184 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
185 /* we don't have page 0 mapped on sparc and m68k.. */
187 unsigned long sz = PAGE_SIZE - p;
190 /* Hmm. Do something? */
200 * Handle first page in case it's not aligned
202 if (-p & (PAGE_SIZE - 1))
203 sz = -p & (PAGE_SIZE - 1);
207 sz = min_t(unsigned long, sz, count);
210 * On ia64 if a page has been mapped somewhere as
211 * uncached, then it must also be accessed uncached
212 * by the kernel or data corruption may occur
214 ptr = xlate_dev_mem_ptr(p);
216 copied = copy_from_user(ptr, buf, sz);
220 ret = written + (sz - copied);
235 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
237 #if defined(__HAVE_PHYS_MEM_ACCESS_PROT)
238 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
240 vma->vm_page_prot = phys_mem_access_prot(file, offset,
241 vma->vm_end - vma->vm_start,
243 #elif defined(pgprot_noncached)
244 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
247 uncached = uncached_access(file, offset);
249 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
252 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
253 if (remap_pfn_range(vma,
256 vma->vm_end-vma->vm_start,
262 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
266 /* Turn a kernel-virtual address into a physical page frame */
267 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
270 * RED-PEN: on some architectures there is more mapped memory
271 * than available in mem_map which pfn_valid checks
272 * for. Perhaps should add a new macro here.
274 * RED-PEN: vmalloc is not supported right now.
280 return mmap_mem(file, vma);
283 #ifdef CONFIG_CRASH_DUMP
285 * Read memory corresponding to the old kernel.
287 static ssize_t read_oldmem(struct file *file, char __user *buf,
288 size_t count, loff_t *ppos)
290 unsigned long pfn, offset;
291 size_t read = 0, csize;
295 pfn = *ppos / PAGE_SIZE;
296 if (pfn > saved_max_pfn)
299 offset = (unsigned long)(*ppos % PAGE_SIZE);
300 if (count > PAGE_SIZE - offset)
301 csize = PAGE_SIZE - offset;
305 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
317 extern long vread(char *buf, char *addr, unsigned long count);
318 extern long vwrite(char *buf, char *addr, unsigned long count);
321 * This function reads the *virtual* memory as seen by the kernel.
323 static ssize_t read_kmem(struct file *file, char __user *buf,
324 size_t count, loff_t *ppos)
326 unsigned long p = *ppos;
327 ssize_t low_count, read, sz;
328 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
331 if (p < (unsigned long) high_memory) {
333 if (count > (unsigned long) high_memory - p)
334 low_count = (unsigned long) high_memory - p;
336 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
337 /* we don't have page 0 mapped on sparc and m68k.. */
338 if (p < PAGE_SIZE && low_count > 0) {
339 size_t tmp = PAGE_SIZE - p;
340 if (tmp > low_count) tmp = low_count;
341 if (clear_user(buf, tmp))
350 while (low_count > 0) {
352 * Handle first page in case it's not aligned
354 if (-p & (PAGE_SIZE - 1))
355 sz = -p & (PAGE_SIZE - 1);
359 sz = min_t(unsigned long, sz, low_count);
362 * On ia64 if a page has been mapped somewhere as
363 * uncached, then it must also be accessed uncached
364 * by the kernel or data corruption may occur
366 kbuf = xlate_dev_kmem_ptr((char *)p);
368 if (copy_to_user(buf, kbuf, sz))
379 kbuf = (char *)__get_free_page(GFP_KERNEL);
387 len = vread(kbuf, (char *)p, len);
390 if (copy_to_user(buf, kbuf, len)) {
391 free_page((unsigned long)kbuf);
399 free_page((unsigned long)kbuf);
406 static inline ssize_t
407 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
408 size_t count, loff_t *ppos)
411 unsigned long copied;
414 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
415 /* we don't have page 0 mapped on sparc and m68k.. */
416 if (realp < PAGE_SIZE) {
417 unsigned long sz = PAGE_SIZE - realp;
420 /* Hmm. Do something? */
432 * Handle first page in case it's not aligned
434 if (-realp & (PAGE_SIZE - 1))
435 sz = -realp & (PAGE_SIZE - 1);
439 sz = min_t(unsigned long, sz, count);
442 * On ia64 if a page has been mapped somewhere as
443 * uncached, then it must also be accessed uncached
444 * by the kernel or data corruption may occur
446 ptr = xlate_dev_kmem_ptr(p);
448 copied = copy_from_user(ptr, buf, sz);
452 ret = written + (sz - copied);
470 * This function writes to the *virtual* memory as seen by the kernel.
472 static ssize_t write_kmem(struct file * file, const char __user * buf,
473 size_t count, loff_t *ppos)
475 unsigned long p = *ppos;
479 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
481 if (p < (unsigned long) high_memory) {
484 if (count > (unsigned long) high_memory - p)
485 wrote = (unsigned long) high_memory - p;
487 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
488 if (written != wrote)
497 kbuf = (char *)__get_free_page(GFP_KERNEL);
499 return wrote ? wrote : -ENOMEM;
506 written = copy_from_user(kbuf, buf, len);
510 free_page((unsigned long)kbuf);
511 ret = wrote + virtr + (len - written);
512 return ret ? ret : -EFAULT;
515 len = vwrite(kbuf, (char *)p, len);
521 free_page((unsigned long)kbuf);
525 return virtr + wrote;
528 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
529 static ssize_t read_port(struct file * file, char __user * buf,
530 size_t count, loff_t *ppos)
532 unsigned long i = *ppos;
533 char __user *tmp = buf;
535 if (!access_ok(VERIFY_WRITE, buf, count))
537 while (count-- > 0 && i < 65536) {
538 if (__put_user(inb(i),tmp) < 0)
547 static ssize_t write_port(struct file * file, const char __user * buf,
548 size_t count, loff_t *ppos)
550 unsigned long i = *ppos;
551 const char __user * tmp = buf;
553 if (!access_ok(VERIFY_READ,buf,count))
555 while (count-- > 0 && i < 65536) {
557 if (__get_user(c, tmp))
568 static ssize_t read_null(struct file * file, char __user * buf,
569 size_t count, loff_t *ppos)
574 static ssize_t write_null(struct file * file, const char __user * buf,
575 size_t count, loff_t *ppos)
582 * For fun, we are using the MMU for this.
584 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
586 struct mm_struct *mm;
587 struct vm_area_struct * vma;
588 unsigned long addr=(unsigned long)buf;
591 /* Oops, this was forgotten before. -ben */
592 down_read(&mm->mmap_sem);
594 /* For private mappings, just map in zero pages. */
595 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
598 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
600 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
602 count = vma->vm_end - addr;
606 zap_page_range(vma, addr, count, NULL);
607 zeromap_page_range(vma, addr, count, PAGE_COPY);
616 up_read(&mm->mmap_sem);
618 /* The shared case is hard. Let's do the conventional zeroing. */
620 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
622 return size + unwritten - PAGE_SIZE;
630 up_read(&mm->mmap_sem);
634 static ssize_t read_zero(struct file * file, char __user * buf,
635 size_t count, loff_t *ppos)
637 unsigned long left, unwritten, written = 0;
642 if (!access_ok(VERIFY_WRITE, buf, count))
647 /* do we want to be clever? Arbitrary cut-off */
648 if (count >= PAGE_SIZE*4) {
649 unsigned long partial;
651 /* How much left of the page? */
652 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
653 unwritten = clear_user(buf, partial);
654 written = partial - unwritten;
659 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
660 written += (left & PAGE_MASK) - unwritten;
663 buf += left & PAGE_MASK;
666 unwritten = clear_user(buf, left);
667 written += left - unwritten;
669 return written ? written : -EFAULT;
672 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
674 if (vma->vm_flags & VM_SHARED)
675 return shmem_zero_setup(vma);
676 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
680 #else /* CONFIG_MMU */
681 static ssize_t read_zero(struct file * file, char * buf,
682 size_t count, loff_t *ppos)
690 chunk = 4096; /* Just for latency reasons */
691 if (clear_user(buf, chunk))
700 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
704 #endif /* CONFIG_MMU */
706 static ssize_t write_full(struct file * file, const char __user * buf,
707 size_t count, loff_t *ppos)
713 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
714 * can fopen() both devices with "a" now. This was previously impossible.
718 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
720 return file->f_pos = 0;
724 * The memory devices use the full 32/64 bits of the offset, and so we cannot
725 * check against negative addresses: they are ok. The return value is weird,
726 * though, in that case (0).
728 * also note that seeking relative to the "end of file" isn't supported:
729 * it has no meaning, so it returns -EINVAL.
731 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
735 down(&file->f_dentry->d_inode->i_sem);
738 file->f_pos = offset;
740 force_successful_syscall_return();
743 file->f_pos += offset;
745 force_successful_syscall_return();
750 up(&file->f_dentry->d_inode->i_sem);
754 static int open_port(struct inode * inode, struct file * filp)
756 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
759 #define zero_lseek null_lseek
760 #define full_lseek null_lseek
761 #define write_zero write_null
762 #define read_full read_zero
763 #define open_mem open_port
764 #define open_kmem open_mem
765 #define open_oldmem open_mem
767 static struct file_operations mem_fops = {
768 .llseek = memory_lseek,
775 static struct file_operations kmem_fops = {
776 .llseek = memory_lseek,
783 static struct file_operations null_fops = {
784 .llseek = null_lseek,
789 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
790 static struct file_operations port_fops = {
791 .llseek = memory_lseek,
798 static struct file_operations zero_fops = {
799 .llseek = zero_lseek,
805 static struct backing_dev_info zero_bdi = {
806 .capabilities = BDI_CAP_MAP_COPY,
809 static struct file_operations full_fops = {
810 .llseek = full_lseek,
815 #ifdef CONFIG_CRASH_DUMP
816 static struct file_operations oldmem_fops = {
822 static ssize_t kmsg_write(struct file * file, const char __user * buf,
823 size_t count, loff_t *ppos)
828 tmp = kmalloc(count + 1, GFP_KERNEL);
832 if (!copy_from_user(tmp, buf, count)) {
834 ret = printk("%s", tmp);
840 static struct file_operations kmsg_fops = {
844 static int memory_open(struct inode * inode, struct file * filp)
846 switch (iminor(inode)) {
848 filp->f_op = &mem_fops;
851 filp->f_op = &kmem_fops;
854 filp->f_op = &null_fops;
856 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
858 filp->f_op = &port_fops;
862 filp->f_mapping->backing_dev_info = &zero_bdi;
863 filp->f_op = &zero_fops;
866 filp->f_op = &full_fops;
869 filp->f_op = &random_fops;
872 filp->f_op = &urandom_fops;
875 filp->f_op = &kmsg_fops;
877 #ifdef CONFIG_CRASH_DUMP
879 filp->f_op = &oldmem_fops;
885 if (filp->f_op && filp->f_op->open)
886 return filp->f_op->open(inode,filp);
890 static struct file_operations memory_fops = {
891 .open = memory_open, /* just a selector for the real open */
894 static const struct {
898 struct file_operations *fops;
899 } devlist[] = { /* list of minor devices */
900 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
901 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
902 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
903 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
904 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
906 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
907 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
908 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
909 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
910 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
911 #ifdef CONFIG_CRASH_DUMP
912 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
916 static struct class *mem_class;
918 static int __init chr_dev_init(void)
922 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
923 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
925 mem_class = class_create(THIS_MODULE, "mem");
926 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
927 class_device_create(mem_class, MKDEV(MEM_MAJOR, devlist[i].minor),
928 NULL, devlist[i].name);
929 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
930 S_IFCHR | devlist[i].mode, devlist[i].name);
936 fs_initcall(chr_dev_init);