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>
30 #include <linux/pipe_fs_i.h>
32 #include <asm/uaccess.h>
36 # include <linux/efi.h>
39 static inline int range_is_allowed(unsigned long from, unsigned long to)
43 cursor = from >> PAGE_SHIFT;
44 while ((cursor << PAGE_SHIFT) < to) {
45 if (!devmem_is_allowed(cursor)) {
46 printk ("Program %s tried to read /dev/mem between %lx->%lx.\n",
47 current->comm, from, to);
56 * Architectures vary in how they handle caching for addresses
57 * outside of main memory.
60 static inline int uncached_access(struct file *file, unsigned long addr)
64 * On the PPro and successors, the MTRRs are used to set
65 * memory types for physical addresses outside main memory,
66 * so blindly setting PCD or PWT on those pages is wrong.
67 * For Pentiums and earlier, the surround logic should disable
68 * caching for the high addresses through the KEN pin, but
69 * we maintain the tradition of paranoia in this code.
71 if (file->f_flags & O_SYNC)
73 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
74 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
75 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
76 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
77 && addr >= __pa(high_memory);
78 #elif defined(__x86_64__)
80 * This is broken because it can generate memory type aliases,
81 * which can cause cache corruptions
82 * But it is only available for root and we have to be bug-to-bug
83 * compatible with i386.
85 if (file->f_flags & O_SYNC)
87 /* same behaviour as i386. PAT always set to cached and MTRRs control the
89 Hopefully a full PAT implementation will fix that soon. */
91 #elif defined(CONFIG_IA64)
93 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
95 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
98 * Accessing memory above the top the kernel knows about or through a file pointer
99 * that was marked O_SYNC will be done non-cached.
101 if (file->f_flags & O_SYNC)
103 return addr >= __pa(high_memory);
107 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
108 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
110 if (addr + count > __pa(high_memory))
116 static inline int valid_mmap_phys_addr_range(unsigned long addr, size_t size)
122 #ifndef ARCH_HAS_DEV_MEM
124 * This funcion reads the *physical* memory. The f_pos points directly to the
127 static ssize_t read_mem(struct file * file, char __user * buf,
128 size_t count, loff_t *ppos)
130 unsigned long p = *ppos;
134 if (!valid_phys_addr_range(p, count))
137 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
138 /* we don't have page 0 mapped on sparc and m68k.. */
144 if (clear_user(buf, sz))
156 * Handle first page in case it's not aligned
158 if (-p & (PAGE_SIZE - 1))
159 sz = -p & (PAGE_SIZE - 1);
163 sz = min_t(unsigned long, sz, count);
166 * On ia64 if a page has been mapped somewhere as
167 * uncached, then it must also be accessed uncached
168 * by the kernel or data corruption may occur
170 ptr = xlate_dev_mem_ptr(p);
172 if (!range_is_allowed(p, p+count))
174 if (copy_to_user(buf, ptr, sz))
186 static ssize_t write_mem(struct file * file, const char __user * buf,
187 size_t count, loff_t *ppos)
189 unsigned long p = *ppos;
191 unsigned long copied;
194 if (!valid_phys_addr_range(p, count))
199 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
200 /* we don't have page 0 mapped on sparc and m68k.. */
202 unsigned long sz = PAGE_SIZE - p;
205 /* Hmm. Do something? */
215 * Handle first page in case it's not aligned
217 if (-p & (PAGE_SIZE - 1))
218 sz = -p & (PAGE_SIZE - 1);
222 sz = min_t(unsigned long, sz, count);
225 * On ia64 if a page has been mapped somewhere as
226 * uncached, then it must also be accessed uncached
227 * by the kernel or data corruption may occur
229 ptr = xlate_dev_mem_ptr(p);
231 if (!range_is_allowed(ptr, ptr+sz))
233 copied = copy_from_user(ptr, buf, sz);
235 written += sz - copied;
251 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
252 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
253 unsigned long size, pgprot_t vma_prot)
255 #ifdef pgprot_noncached
256 unsigned long offset = pfn << PAGE_SHIFT;
258 if (uncached_access(file, offset))
259 return pgprot_noncached(vma_prot);
265 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
267 size_t size = vma->vm_end - vma->vm_start;
269 if (!valid_mmap_phys_addr_range(vma->vm_pgoff << PAGE_SHIFT, size))
272 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
276 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
277 if (remap_pfn_range(vma,
286 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
290 /* Turn a kernel-virtual address into a physical page frame */
291 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
294 * RED-PEN: on some architectures there is more mapped memory
295 * than available in mem_map which pfn_valid checks
296 * for. Perhaps should add a new macro here.
298 * RED-PEN: vmalloc is not supported right now.
304 return mmap_mem(file, vma);
307 #ifdef CONFIG_CRASH_DUMP
309 * Read memory corresponding to the old kernel.
311 static ssize_t read_oldmem(struct file *file, char __user *buf,
312 size_t count, loff_t *ppos)
314 unsigned long pfn, offset;
315 size_t read = 0, csize;
319 pfn = *ppos / PAGE_SIZE;
320 if (pfn > saved_max_pfn)
323 offset = (unsigned long)(*ppos % PAGE_SIZE);
324 if (count > PAGE_SIZE - offset)
325 csize = PAGE_SIZE - offset;
329 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
341 extern long vread(char *buf, char *addr, unsigned long count);
342 extern long vwrite(char *buf, char *addr, unsigned long count);
345 * This function reads the *virtual* memory as seen by the kernel.
347 static ssize_t read_kmem(struct file *file, char __user *buf,
348 size_t count, loff_t *ppos)
350 unsigned long p = *ppos;
351 ssize_t low_count, read, sz;
352 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
357 if (p < (unsigned long) high_memory) {
359 if (count > (unsigned long) high_memory - p)
360 low_count = (unsigned long) high_memory - p;
362 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
363 /* we don't have page 0 mapped on sparc and m68k.. */
364 if (p < PAGE_SIZE && low_count > 0) {
365 size_t tmp = PAGE_SIZE - p;
366 if (tmp > low_count) tmp = low_count;
367 if (clear_user(buf, tmp))
376 while (low_count > 0) {
378 * Handle first page in case it's not aligned
380 if (-p & (PAGE_SIZE - 1))
381 sz = -p & (PAGE_SIZE - 1);
385 sz = min_t(unsigned long, sz, low_count);
388 * On ia64 if a page has been mapped somewhere as
389 * uncached, then it must also be accessed uncached
390 * by the kernel or data corruption may occur
392 kbuf = xlate_dev_kmem_ptr((char *)p);
394 if (copy_to_user(buf, kbuf, sz))
405 kbuf = (char *)__get_free_page(GFP_KERNEL);
413 len = vread(kbuf, (char *)p, len);
416 if (copy_to_user(buf, kbuf, len)) {
417 free_page((unsigned long)kbuf);
425 free_page((unsigned long)kbuf);
432 #if defined(CONFIG_ISA) || !defined(__mc68000__)
433 static ssize_t read_port(struct file * file, char __user * buf,
434 size_t count, loff_t *ppos)
436 unsigned long i = *ppos;
437 char __user *tmp = buf;
439 if (!access_ok(VERIFY_WRITE, buf, count))
441 while (count-- > 0 && i < 65536) {
442 if (__put_user(inb(i),tmp) < 0)
451 static ssize_t write_port(struct file * file, const char __user * buf,
452 size_t count, loff_t *ppos)
454 unsigned long i = *ppos;
455 const char __user * tmp = buf;
457 if (!access_ok(VERIFY_READ,buf,count))
459 while (count-- > 0 && i < 65536) {
461 if (__get_user(c, tmp)) {
475 static ssize_t read_null(struct file * file, char __user * buf,
476 size_t count, loff_t *ppos)
481 static ssize_t write_null(struct file * file, const char __user * buf,
482 size_t count, loff_t *ppos)
487 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
488 struct splice_desc *sd)
493 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
494 loff_t *ppos, size_t len, unsigned int flags)
496 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
501 * For fun, we are using the MMU for this.
503 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
505 struct mm_struct *mm;
506 struct vm_area_struct * vma;
507 unsigned long addr=(unsigned long)buf;
510 /* Oops, this was forgotten before. -ben */
511 down_read(&mm->mmap_sem);
513 /* For private mappings, just map in zero pages. */
514 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
517 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
519 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
521 count = vma->vm_end - addr;
525 zap_page_range(vma, addr, count, NULL);
526 zeromap_page_range(vma, addr, count, PAGE_COPY);
535 up_read(&mm->mmap_sem);
537 /* The shared case is hard. Let's do the conventional zeroing. */
539 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
541 return size + unwritten - PAGE_SIZE;
549 up_read(&mm->mmap_sem);
553 static ssize_t read_zero(struct file * file, char __user * buf,
554 size_t count, loff_t *ppos)
556 unsigned long left, unwritten, written = 0;
561 if (!access_ok(VERIFY_WRITE, buf, count))
566 /* do we want to be clever? Arbitrary cut-off */
567 if (count >= PAGE_SIZE*4) {
568 unsigned long partial;
570 /* How much left of the page? */
571 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
572 unwritten = clear_user(buf, partial);
573 written = partial - unwritten;
578 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
579 written += (left & PAGE_MASK) - unwritten;
582 buf += left & PAGE_MASK;
585 unwritten = clear_user(buf, left);
586 written += left - unwritten;
588 return written ? written : -EFAULT;
591 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
593 if (vma->vm_flags & VM_SHARED)
594 return shmem_zero_setup(vma);
595 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
599 #else /* CONFIG_MMU */
600 static ssize_t read_zero(struct file * file, char * buf,
601 size_t count, loff_t *ppos)
609 chunk = 4096; /* Just for latency reasons */
610 if (clear_user(buf, chunk))
619 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
623 #endif /* CONFIG_MMU */
625 static ssize_t write_full(struct file * file, const char __user * buf,
626 size_t count, loff_t *ppos)
632 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
633 * can fopen() both devices with "a" now. This was previously impossible.
637 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
639 return file->f_pos = 0;
643 * The memory devices use the full 32/64 bits of the offset, and so we cannot
644 * check against negative addresses: they are ok. The return value is weird,
645 * though, in that case (0).
647 * also note that seeking relative to the "end of file" isn't supported:
648 * it has no meaning, so it returns -EINVAL.
650 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
654 mutex_lock(&file->f_dentry->d_inode->i_mutex);
657 file->f_pos = offset;
659 force_successful_syscall_return();
662 file->f_pos += offset;
664 force_successful_syscall_return();
669 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
673 static int open_port(struct inode * inode, struct file * filp)
675 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
678 #define zero_lseek null_lseek
679 #define full_lseek null_lseek
680 #define write_zero write_null
681 #define read_full read_zero
682 #define open_mem open_port
683 #define open_kmem open_mem
684 #define open_oldmem open_mem
686 #ifndef ARCH_HAS_DEV_MEM
687 static struct file_operations mem_fops = {
688 .llseek = memory_lseek,
695 extern struct file_operations mem_fops;
698 static struct file_operations kmem_fops = {
699 .llseek = memory_lseek,
705 static struct file_operations null_fops = {
706 .llseek = null_lseek,
709 .splice_write = splice_write_null,
712 #if defined(CONFIG_ISA) || !defined(__mc68000__)
713 static struct file_operations port_fops = {
714 .llseek = memory_lseek,
721 static struct file_operations zero_fops = {
722 .llseek = zero_lseek,
728 static struct backing_dev_info zero_bdi = {
729 .capabilities = BDI_CAP_MAP_COPY,
732 static struct file_operations full_fops = {
733 .llseek = full_lseek,
738 #ifdef CONFIG_CRASH_DUMP
739 static struct file_operations oldmem_fops = {
745 static ssize_t kmsg_write(struct file * file, const char __user * buf,
746 size_t count, loff_t *ppos)
751 tmp = kmalloc(count + 1, GFP_KERNEL);
755 if (!copy_from_user(tmp, buf, count)) {
757 ret = printk("%s", tmp);
759 /* printk can add a prefix */
766 static struct file_operations kmsg_fops = {
770 static int memory_open(struct inode * inode, struct file * filp)
772 switch (iminor(inode)) {
774 filp->f_op = &mem_fops;
777 filp->f_op = &kmem_fops;
780 filp->f_op = &null_fops;
782 #if defined(CONFIG_ISA) || !defined(__mc68000__)
784 filp->f_op = &port_fops;
788 filp->f_mapping->backing_dev_info = &zero_bdi;
789 filp->f_op = &zero_fops;
792 filp->f_op = &full_fops;
795 filp->f_op = &random_fops;
798 filp->f_op = &urandom_fops;
801 filp->f_op = &kmsg_fops;
803 #ifdef CONFIG_CRASH_DUMP
805 filp->f_op = &oldmem_fops;
811 if (filp->f_op && filp->f_op->open)
812 return filp->f_op->open(inode,filp);
816 static struct file_operations memory_fops = {
817 .open = memory_open, /* just a selector for the real open */
820 static const struct {
824 const struct file_operations *fops;
825 } devlist[] = { /* list of minor devices */
826 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
827 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
828 #if defined(CONFIG_ISA) || !defined(__mc68000__)
829 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
831 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
832 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
833 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
834 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
835 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
836 #ifdef CONFIG_CRASH_DUMP
837 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
841 static struct class *mem_class;
843 static int __init chr_dev_init(void)
847 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
848 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
850 mem_class = class_create(THIS_MODULE, "mem");
851 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
852 class_device_create(mem_class, NULL,
853 MKDEV(MEM_MAJOR, devlist[i].minor),
854 NULL, devlist[i].name);
855 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
856 S_IFCHR | devlist[i].mode, devlist[i].name);
862 fs_initcall(chr_dev_init);