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/ptrace.h>
24 #include <linux/device.h>
25 #include <linux/highmem.h>
26 #include <linux/crash_dump.h>
27 #include <linux/backing-dev.h>
28 #include <linux/bootmem.h>
29 #include <linux/pipe_fs_i.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 if (addr + count > __pa(high_memory))
99 static inline int valid_mmap_phys_addr_range(unsigned long addr, size_t size)
106 * This funcion reads the *physical* memory. The f_pos points directly to the
109 static ssize_t read_mem(struct file * file, char __user * buf,
110 size_t count, loff_t *ppos)
112 unsigned long p = *ppos;
116 if (!valid_phys_addr_range(p, count))
119 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
120 /* we don't have page 0 mapped on sparc and m68k.. */
126 if (clear_user(buf, sz))
138 * Handle first page in case it's not aligned
140 if (-p & (PAGE_SIZE - 1))
141 sz = -p & (PAGE_SIZE - 1);
145 sz = min_t(unsigned long, sz, count);
148 * On ia64 if a page has been mapped somewhere as
149 * uncached, then it must also be accessed uncached
150 * by the kernel or data corruption may occur
152 ptr = xlate_dev_mem_ptr(p);
154 if (copy_to_user(buf, ptr, sz))
166 static ssize_t write_mem(struct file * file, const char __user * buf,
167 size_t count, loff_t *ppos)
169 unsigned long p = *ppos;
171 unsigned long copied;
174 if (!valid_phys_addr_range(p, count))
179 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
180 /* we don't have page 0 mapped on sparc and m68k.. */
182 unsigned long sz = PAGE_SIZE - p;
185 /* Hmm. Do something? */
195 * Handle first page in case it's not aligned
197 if (-p & (PAGE_SIZE - 1))
198 sz = -p & (PAGE_SIZE - 1);
202 sz = min_t(unsigned long, sz, count);
205 * On ia64 if a page has been mapped somewhere as
206 * uncached, then it must also be accessed uncached
207 * by the kernel or data corruption may occur
209 ptr = xlate_dev_mem_ptr(p);
211 copied = copy_from_user(ptr, buf, sz);
213 written += sz - copied;
228 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
229 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
230 unsigned long size, pgprot_t vma_prot)
232 #ifdef pgprot_noncached
233 unsigned long offset = pfn << PAGE_SHIFT;
235 if (uncached_access(file, offset))
236 return pgprot_noncached(vma_prot);
242 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
244 size_t size = vma->vm_end - vma->vm_start;
246 if (!valid_mmap_phys_addr_range(vma->vm_pgoff << PAGE_SHIFT, size))
249 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
253 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
254 if (remap_pfn_range(vma,
263 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
267 /* Turn a kernel-virtual address into a physical page frame */
268 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
271 * RED-PEN: on some architectures there is more mapped memory
272 * than available in mem_map which pfn_valid checks
273 * for. Perhaps should add a new macro here.
275 * RED-PEN: vmalloc is not supported right now.
281 return mmap_mem(file, vma);
284 #ifdef CONFIG_CRASH_DUMP
286 * Read memory corresponding to the old kernel.
288 static ssize_t read_oldmem(struct file *file, char __user *buf,
289 size_t count, loff_t *ppos)
291 unsigned long pfn, offset;
292 size_t read = 0, csize;
296 pfn = *ppos / PAGE_SIZE;
297 if (pfn > saved_max_pfn)
300 offset = (unsigned long)(*ppos % PAGE_SIZE);
301 if (count > PAGE_SIZE - offset)
302 csize = PAGE_SIZE - offset;
306 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
318 extern long vread(char *buf, char *addr, unsigned long count);
319 extern long vwrite(char *buf, char *addr, unsigned long count);
322 * This function reads the *virtual* memory as seen by the kernel.
324 static ssize_t read_kmem(struct file *file, char __user *buf,
325 size_t count, loff_t *ppos)
327 unsigned long p = *ppos;
328 ssize_t low_count, read, sz;
329 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
332 if (p < (unsigned long) high_memory) {
334 if (count > (unsigned long) high_memory - p)
335 low_count = (unsigned long) high_memory - p;
337 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
338 /* we don't have page 0 mapped on sparc and m68k.. */
339 if (p < PAGE_SIZE && low_count > 0) {
340 size_t tmp = PAGE_SIZE - p;
341 if (tmp > low_count) tmp = low_count;
342 if (clear_user(buf, tmp))
351 while (low_count > 0) {
353 * Handle first page in case it's not aligned
355 if (-p & (PAGE_SIZE - 1))
356 sz = -p & (PAGE_SIZE - 1);
360 sz = min_t(unsigned long, sz, low_count);
363 * On ia64 if a page has been mapped somewhere as
364 * uncached, then it must also be accessed uncached
365 * by the kernel or data corruption may occur
367 kbuf = xlate_dev_kmem_ptr((char *)p);
369 if (copy_to_user(buf, kbuf, sz))
380 kbuf = (char *)__get_free_page(GFP_KERNEL);
388 len = vread(kbuf, (char *)p, len);
391 if (copy_to_user(buf, kbuf, len)) {
392 free_page((unsigned long)kbuf);
400 free_page((unsigned long)kbuf);
407 static inline ssize_t
408 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
409 size_t count, loff_t *ppos)
412 unsigned long copied;
415 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
416 /* we don't have page 0 mapped on sparc and m68k.. */
417 if (realp < PAGE_SIZE) {
418 unsigned long sz = PAGE_SIZE - realp;
421 /* Hmm. Do something? */
433 * Handle first page in case it's not aligned
435 if (-realp & (PAGE_SIZE - 1))
436 sz = -realp & (PAGE_SIZE - 1);
440 sz = min_t(unsigned long, sz, count);
443 * On ia64 if a page has been mapped somewhere as
444 * uncached, then it must also be accessed uncached
445 * by the kernel or data corruption may occur
447 ptr = xlate_dev_kmem_ptr(p);
449 copied = copy_from_user(ptr, buf, sz);
451 written += sz - copied;
469 * This function writes to the *virtual* memory as seen by the kernel.
471 static ssize_t write_kmem(struct file * file, const char __user * buf,
472 size_t count, loff_t *ppos)
474 unsigned long p = *ppos;
478 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
480 if (p < (unsigned long) high_memory) {
483 if (count > (unsigned long) high_memory - p)
484 wrote = (unsigned long) high_memory - p;
486 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
487 if (written != wrote)
496 kbuf = (char *)__get_free_page(GFP_KERNEL);
498 return wrote ? wrote : -ENOMEM;
505 written = copy_from_user(kbuf, buf, len);
509 free_page((unsigned long)kbuf);
513 len = vwrite(kbuf, (char *)p, len);
519 free_page((unsigned long)kbuf);
523 return virtr + wrote;
526 #if defined(CONFIG_ISA) || !defined(__mc68000__)
527 static ssize_t read_port(struct file * file, char __user * buf,
528 size_t count, loff_t *ppos)
530 unsigned long i = *ppos;
531 char __user *tmp = buf;
533 if (!access_ok(VERIFY_WRITE, buf, count))
535 while (count-- > 0 && i < 65536) {
536 if (__put_user(inb(i),tmp) < 0)
545 static ssize_t write_port(struct file * file, const char __user * buf,
546 size_t count, loff_t *ppos)
548 unsigned long i = *ppos;
549 const char __user * tmp = buf;
551 if (!access_ok(VERIFY_READ,buf,count))
553 while (count-- > 0 && i < 65536) {
555 if (__get_user(c, tmp)) {
569 static ssize_t read_null(struct file * file, char __user * buf,
570 size_t count, loff_t *ppos)
575 static ssize_t write_null(struct file * file, const char __user * buf,
576 size_t count, loff_t *ppos)
581 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
582 struct splice_desc *sd)
587 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
588 loff_t *ppos, size_t len, unsigned int flags)
590 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
595 * For fun, we are using the MMU for this.
597 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
599 struct mm_struct *mm;
600 struct vm_area_struct * vma;
601 unsigned long addr=(unsigned long)buf;
604 /* Oops, this was forgotten before. -ben */
605 down_read(&mm->mmap_sem);
607 /* For private mappings, just map in zero pages. */
608 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
611 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
613 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
615 count = vma->vm_end - addr;
619 zap_page_range(vma, addr, count, NULL);
620 zeromap_page_range(vma, addr, count, PAGE_COPY);
629 up_read(&mm->mmap_sem);
631 /* The shared case is hard. Let's do the conventional zeroing. */
633 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
635 return size + unwritten - PAGE_SIZE;
643 up_read(&mm->mmap_sem);
647 static ssize_t read_zero(struct file * file, char __user * buf,
648 size_t count, loff_t *ppos)
650 unsigned long left, unwritten, written = 0;
655 if (!access_ok(VERIFY_WRITE, buf, count))
660 /* do we want to be clever? Arbitrary cut-off */
661 if (count >= PAGE_SIZE*4) {
662 unsigned long partial;
664 /* How much left of the page? */
665 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
666 unwritten = clear_user(buf, partial);
667 written = partial - unwritten;
672 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
673 written += (left & PAGE_MASK) - unwritten;
676 buf += left & PAGE_MASK;
679 unwritten = clear_user(buf, left);
680 written += left - unwritten;
682 return written ? written : -EFAULT;
685 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
687 if (vma->vm_flags & VM_SHARED)
688 return shmem_zero_setup(vma);
689 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
693 #else /* CONFIG_MMU */
694 static ssize_t read_zero(struct file * file, char * buf,
695 size_t count, loff_t *ppos)
703 chunk = 4096; /* Just for latency reasons */
704 if (clear_user(buf, chunk))
713 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
717 #endif /* CONFIG_MMU */
719 static ssize_t write_full(struct file * file, const char __user * buf,
720 size_t count, loff_t *ppos)
726 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
727 * can fopen() both devices with "a" now. This was previously impossible.
731 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
733 return file->f_pos = 0;
737 * The memory devices use the full 32/64 bits of the offset, and so we cannot
738 * check against negative addresses: they are ok. The return value is weird,
739 * though, in that case (0).
741 * also note that seeking relative to the "end of file" isn't supported:
742 * it has no meaning, so it returns -EINVAL.
744 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
748 mutex_lock(&file->f_dentry->d_inode->i_mutex);
751 file->f_pos = offset;
753 force_successful_syscall_return();
756 file->f_pos += offset;
758 force_successful_syscall_return();
763 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
767 static int open_port(struct inode * inode, struct file * filp)
769 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
772 #define zero_lseek null_lseek
773 #define full_lseek null_lseek
774 #define write_zero write_null
775 #define read_full read_zero
776 #define open_mem open_port
777 #define open_kmem open_mem
778 #define open_oldmem open_mem
780 static struct file_operations mem_fops = {
781 .llseek = memory_lseek,
788 static struct file_operations kmem_fops = {
789 .llseek = memory_lseek,
796 static struct file_operations null_fops = {
797 .llseek = null_lseek,
800 .splice_write = splice_write_null,
803 #if defined(CONFIG_ISA) || !defined(__mc68000__)
804 static struct file_operations port_fops = {
805 .llseek = memory_lseek,
812 static struct file_operations zero_fops = {
813 .llseek = zero_lseek,
819 static struct backing_dev_info zero_bdi = {
820 .capabilities = BDI_CAP_MAP_COPY,
823 static struct file_operations full_fops = {
824 .llseek = full_lseek,
829 #ifdef CONFIG_CRASH_DUMP
830 static struct file_operations oldmem_fops = {
836 static ssize_t kmsg_write(struct file * file, const char __user * buf,
837 size_t count, loff_t *ppos)
842 tmp = kmalloc(count + 1, GFP_KERNEL);
846 if (!copy_from_user(tmp, buf, count)) {
848 ret = printk("%s", tmp);
850 /* printk can add a prefix */
857 static struct file_operations kmsg_fops = {
861 static int memory_open(struct inode * inode, struct file * filp)
863 switch (iminor(inode)) {
865 filp->f_op = &mem_fops;
868 filp->f_op = &kmem_fops;
871 filp->f_op = &null_fops;
873 #if defined(CONFIG_ISA) || !defined(__mc68000__)
875 filp->f_op = &port_fops;
879 filp->f_mapping->backing_dev_info = &zero_bdi;
880 filp->f_op = &zero_fops;
883 filp->f_op = &full_fops;
886 filp->f_op = &random_fops;
889 filp->f_op = &urandom_fops;
892 filp->f_op = &kmsg_fops;
894 #ifdef CONFIG_CRASH_DUMP
896 filp->f_op = &oldmem_fops;
902 if (filp->f_op && filp->f_op->open)
903 return filp->f_op->open(inode,filp);
907 static struct file_operations memory_fops = {
908 .open = memory_open, /* just a selector for the real open */
911 static const struct {
915 const struct file_operations *fops;
916 } devlist[] = { /* list of minor devices */
917 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
918 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
919 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
920 #if defined(CONFIG_ISA) || !defined(__mc68000__)
921 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
923 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
924 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
925 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
926 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
927 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
928 #ifdef CONFIG_CRASH_DUMP
929 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
933 static struct class *mem_class;
935 static int __init chr_dev_init(void)
939 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
940 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
942 mem_class = class_create(THIS_MODULE, "mem");
943 for (i = 0; i < ARRAY_SIZE(devlist); i++)
944 class_device_create(mem_class, NULL,
945 MKDEV(MEM_MAJOR, devlist[i].minor),
946 NULL, devlist[i].name);
951 fs_initcall(chr_dev_init);