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 pfn, unsigned long size)
114 u64 from = ((u64)pfn) << PAGE_SHIFT;
115 u64 to = from + size;
118 while (cursor < to) {
119 if (!devmem_is_allowed(pfn)) {
121 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
122 current->comm, from, to);
131 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
138 * This funcion reads the *physical* memory. The f_pos points directly to the
141 static ssize_t read_mem(struct file * file, char __user * buf,
142 size_t count, loff_t *ppos)
144 unsigned long p = *ppos;
148 if (!valid_phys_addr_range(p, count))
151 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
152 /* we don't have page 0 mapped on sparc and m68k.. */
158 if (clear_user(buf, sz))
170 * Handle first page in case it's not aligned
172 if (-p & (PAGE_SIZE - 1))
173 sz = -p & (PAGE_SIZE - 1);
177 sz = min_t(unsigned long, sz, count);
180 * On ia64 if a page has been mapped somewhere as
181 * uncached, then it must also be accessed uncached
182 * by the kernel or data corruption may occur
184 ptr = xlate_dev_mem_ptr(p);
186 if (!range_is_allowed(p >> PAGE_SHIFT, count))
188 if (copy_to_user(buf, ptr, sz))
200 static ssize_t write_mem(struct file * file, const char __user * buf,
201 size_t count, loff_t *ppos)
203 unsigned long p = *ppos;
205 unsigned long copied;
208 if (!valid_phys_addr_range(p, count))
213 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
214 /* we don't have page 0 mapped on sparc and m68k.. */
216 unsigned long sz = PAGE_SIZE - p;
219 /* Hmm. Do something? */
229 * Handle first page in case it's not aligned
231 if (-p & (PAGE_SIZE - 1))
232 sz = -p & (PAGE_SIZE - 1);
236 sz = min_t(unsigned long, sz, count);
239 * On ia64 if a page has been mapped somewhere as
240 * uncached, then it must also be accessed uncached
241 * by the kernel or data corruption may occur
243 ptr = xlate_dev_mem_ptr(p);
245 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
247 copied = copy_from_user(ptr, buf, sz);
249 written += sz - copied;
264 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
265 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
266 unsigned long size, pgprot_t vma_prot)
268 #ifdef pgprot_noncached
269 unsigned long offset = pfn << PAGE_SHIFT;
271 if (uncached_access(file, offset))
272 return pgprot_noncached(vma_prot);
279 static unsigned long get_unmapped_area_mem(struct file *file,
285 if (!valid_mmap_phys_addr_range(pgoff, len))
286 return (unsigned long) -EINVAL;
287 return pgoff << PAGE_SHIFT;
290 /* can't do an in-place private mapping if there's no MMU */
291 static inline int private_mapping_ok(struct vm_area_struct *vma)
293 return vma->vm_flags & VM_MAYSHARE;
296 #define get_unmapped_area_mem NULL
298 static inline int private_mapping_ok(struct vm_area_struct *vma)
304 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
306 size_t size = vma->vm_end - vma->vm_start;
308 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
311 if (!private_mapping_ok(vma))
314 if (!range_is_allowed(vma->vm_pgoff, size))
317 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
321 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
322 if (remap_pfn_range(vma,
331 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
335 /* Turn a kernel-virtual address into a physical page frame */
336 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
339 * RED-PEN: on some architectures there is more mapped memory
340 * than available in mem_map which pfn_valid checks
341 * for. Perhaps should add a new macro here.
343 * RED-PEN: vmalloc is not supported right now.
349 return mmap_mem(file, vma);
352 #ifdef CONFIG_CRASH_DUMP
354 * Read memory corresponding to the old kernel.
356 static ssize_t read_oldmem(struct file *file, char __user *buf,
357 size_t count, loff_t *ppos)
359 unsigned long pfn, offset;
360 size_t read = 0, csize;
364 pfn = *ppos / PAGE_SIZE;
365 if (pfn > saved_max_pfn)
368 offset = (unsigned long)(*ppos % PAGE_SIZE);
369 if (count > PAGE_SIZE - offset)
370 csize = PAGE_SIZE - offset;
374 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
386 extern long vread(char *buf, char *addr, unsigned long count);
387 extern long vwrite(char *buf, char *addr, unsigned long count);
390 * This function reads the *virtual* memory as seen by the kernel.
392 static ssize_t read_kmem(struct file *file, char __user *buf,
393 size_t count, loff_t *ppos)
395 unsigned long p = *ppos;
396 ssize_t low_count, read, sz;
397 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
400 if (p < (unsigned long) high_memory) {
402 if (count > (unsigned long) high_memory - p)
403 low_count = (unsigned long) high_memory - p;
405 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
406 /* we don't have page 0 mapped on sparc and m68k.. */
407 if (p < PAGE_SIZE && low_count > 0) {
408 size_t tmp = PAGE_SIZE - p;
409 if (tmp > low_count) tmp = low_count;
410 if (clear_user(buf, tmp))
419 while (low_count > 0) {
421 * Handle first page in case it's not aligned
423 if (-p & (PAGE_SIZE - 1))
424 sz = -p & (PAGE_SIZE - 1);
428 sz = min_t(unsigned long, sz, low_count);
431 * On ia64 if a page has been mapped somewhere as
432 * uncached, then it must also be accessed uncached
433 * by the kernel or data corruption may occur
435 kbuf = xlate_dev_kmem_ptr((char *)p);
437 if (copy_to_user(buf, kbuf, sz))
448 kbuf = (char *)__get_free_page(GFP_KERNEL);
456 len = vread(kbuf, (char *)p, len);
459 if (copy_to_user(buf, kbuf, len)) {
460 free_page((unsigned long)kbuf);
468 free_page((unsigned long)kbuf);
475 static inline ssize_t
476 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
477 size_t count, loff_t *ppos)
480 unsigned long copied;
483 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
484 /* we don't have page 0 mapped on sparc and m68k.. */
485 if (realp < PAGE_SIZE) {
486 unsigned long sz = PAGE_SIZE - realp;
489 /* Hmm. Do something? */
501 * Handle first page in case it's not aligned
503 if (-realp & (PAGE_SIZE - 1))
504 sz = -realp & (PAGE_SIZE - 1);
508 sz = min_t(unsigned long, sz, count);
511 * On ia64 if a page has been mapped somewhere as
512 * uncached, then it must also be accessed uncached
513 * by the kernel or data corruption may occur
515 ptr = xlate_dev_kmem_ptr(p);
517 copied = copy_from_user(ptr, buf, sz);
519 written += sz - copied;
537 * This function writes to the *virtual* memory as seen by the kernel.
539 static ssize_t write_kmem(struct file * file, const char __user * buf,
540 size_t count, loff_t *ppos)
542 unsigned long p = *ppos;
546 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
548 if (p < (unsigned long) high_memory) {
551 if (count > (unsigned long) high_memory - p)
552 wrote = (unsigned long) high_memory - p;
554 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
555 if (written != wrote)
564 kbuf = (char *)__get_free_page(GFP_KERNEL);
566 return wrote ? wrote : -ENOMEM;
573 written = copy_from_user(kbuf, buf, len);
577 free_page((unsigned long)kbuf);
581 len = vwrite(kbuf, (char *)p, len);
587 free_page((unsigned long)kbuf);
591 return virtr + wrote;
594 #ifdef CONFIG_DEVPORT
595 static ssize_t read_port(struct file * file, char __user * buf,
596 size_t count, loff_t *ppos)
598 unsigned long i = *ppos;
599 char __user *tmp = buf;
601 if (!access_ok(VERIFY_WRITE, buf, count))
603 while (count-- > 0 && i < 65536) {
604 if (__put_user(inb(i),tmp) < 0)
613 static ssize_t write_port(struct file * file, const char __user * buf,
614 size_t count, loff_t *ppos)
616 unsigned long i = *ppos;
617 const char __user * tmp = buf;
619 if (!access_ok(VERIFY_READ,buf,count))
621 while (count-- > 0 && i < 65536) {
623 if (__get_user(c, tmp)) {
637 static ssize_t read_null(struct file * file, char __user * buf,
638 size_t count, loff_t *ppos)
643 static ssize_t write_null(struct file * file, const char __user * buf,
644 size_t count, loff_t *ppos)
649 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
650 struct splice_desc *sd)
655 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
656 loff_t *ppos, size_t len, unsigned int flags)
658 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
661 static ssize_t read_zero(struct file * file, char __user * buf,
662 size_t count, loff_t *ppos)
669 if (!access_ok(VERIFY_WRITE, buf, count))
674 unsigned long unwritten;
675 size_t chunk = count;
677 if (chunk > PAGE_SIZE)
678 chunk = PAGE_SIZE; /* Just for latency reasons */
679 unwritten = clear_user(buf, chunk);
680 written += chunk - unwritten;
687 return written ? written : -EFAULT;
690 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
695 if (vma->vm_flags & VM_SHARED)
696 return shmem_zero_setup(vma);
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 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
732 file->f_pos = offset;
734 force_successful_syscall_return();
737 file->f_pos += offset;
739 force_successful_syscall_return();
744 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
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 const struct file_operations mem_fops = {
762 .llseek = memory_lseek,
767 .get_unmapped_area = get_unmapped_area_mem,
770 static const struct file_operations kmem_fops = {
771 .llseek = memory_lseek,
776 .get_unmapped_area = get_unmapped_area_mem,
779 static const struct file_operations null_fops = {
780 .llseek = null_lseek,
783 .splice_write = splice_write_null,
786 #ifdef CONFIG_DEVPORT
787 static const struct file_operations port_fops = {
788 .llseek = memory_lseek,
795 static const struct file_operations zero_fops = {
796 .llseek = zero_lseek,
803 * capabilities for /dev/zero
804 * - permits private mappings, "copies" are taken of the source of zeros
806 static struct backing_dev_info zero_bdi = {
807 .capabilities = BDI_CAP_MAP_COPY,
810 static const struct file_operations full_fops = {
811 .llseek = full_lseek,
816 #ifdef CONFIG_CRASH_DUMP
817 static const struct file_operations oldmem_fops = {
823 static ssize_t kmsg_write(struct file * file, const char __user * buf,
824 size_t count, loff_t *ppos)
829 tmp = kmalloc(count + 1, GFP_KERNEL);
833 if (!copy_from_user(tmp, buf, count)) {
835 ret = printk("%s", tmp);
837 /* printk can add a prefix */
844 static const struct file_operations kmsg_fops = {
848 static int memory_open(struct inode * inode, struct file * filp)
850 switch (iminor(inode)) {
852 filp->f_op = &mem_fops;
853 filp->f_mapping->backing_dev_info =
854 &directly_mappable_cdev_bdi;
857 filp->f_op = &kmem_fops;
858 filp->f_mapping->backing_dev_info =
859 &directly_mappable_cdev_bdi;
862 filp->f_op = &null_fops;
864 #ifdef CONFIG_DEVPORT
866 filp->f_op = &port_fops;
870 filp->f_mapping->backing_dev_info = &zero_bdi;
871 filp->f_op = &zero_fops;
874 filp->f_op = &full_fops;
877 filp->f_op = &random_fops;
880 filp->f_op = &urandom_fops;
883 filp->f_op = &kmsg_fops;
885 #ifdef CONFIG_CRASH_DUMP
887 filp->f_op = &oldmem_fops;
893 if (filp->f_op && filp->f_op->open)
894 return filp->f_op->open(inode,filp);
898 static const struct file_operations memory_fops = {
899 .open = memory_open, /* just a selector for the real open */
902 static const struct {
906 const struct file_operations *fops;
907 } devlist[] = { /* list of minor devices */
908 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
909 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
910 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
911 #ifdef CONFIG_DEVPORT
912 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
914 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
915 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
916 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
917 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
918 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
919 #ifdef CONFIG_CRASH_DUMP
920 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
924 static struct class *mem_class;
926 static int __init chr_dev_init(void)
931 err = bdi_init(&zero_bdi);
935 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
936 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
938 mem_class = class_create(THIS_MODULE, "mem");
939 for (i = 0; i < ARRAY_SIZE(devlist); i++)
940 device_create(mem_class, NULL,
941 MKDEV(MEM_MAJOR, devlist[i].minor),
947 fs_initcall(chr_dev_init);