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
)
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__)
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
)
112 * This funcion reads the *physical* memory. The f_pos points directly to the
115 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
116 size_t count
, loff_t
*ppos
)
118 unsigned long p
= *ppos
;
122 if (!valid_phys_addr_range(p
, count
))
125 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
126 /* we don't have page 0 mapped on sparc and m68k.. */
132 if (clear_user(buf
, sz
))
144 * Handle first page in case it's not aligned
146 if (-p
& (PAGE_SIZE
- 1))
147 sz
= -p
& (PAGE_SIZE
- 1);
151 sz
= min_t(unsigned long, sz
, count
);
154 * On ia64 if a page has been mapped somewhere as
155 * uncached, then it must also be accessed uncached
156 * by the kernel or data corruption may occur
158 ptr
= xlate_dev_mem_ptr(p
);
160 if (copy_to_user(buf
, ptr
, sz
))
172 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
173 size_t count
, loff_t
*ppos
)
175 unsigned long p
= *ppos
;
177 unsigned long copied
;
180 if (!valid_phys_addr_range(p
, count
))
185 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
186 /* we don't have page 0 mapped on sparc and m68k.. */
188 unsigned long sz
= PAGE_SIZE
- p
;
191 /* Hmm. Do something? */
201 * Handle first page in case it's not aligned
203 if (-p
& (PAGE_SIZE
- 1))
204 sz
= -p
& (PAGE_SIZE
- 1);
208 sz
= min_t(unsigned long, sz
, count
);
211 * On ia64 if a page has been mapped somewhere as
212 * uncached, then it must also be accessed uncached
213 * by the kernel or data corruption may occur
215 ptr
= xlate_dev_mem_ptr(p
);
217 copied
= copy_from_user(ptr
, buf
, sz
);
219 written
+= sz
- copied
;
234 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
235 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
236 unsigned long size
, pgprot_t vma_prot
)
238 #ifdef pgprot_noncached
239 unsigned long offset
= pfn
<< PAGE_SHIFT
;
241 if (uncached_access(file
, offset
))
242 return pgprot_noncached(vma_prot
);
249 static unsigned long get_unmapped_area_mem(struct file
*file
,
255 if (!valid_mmap_phys_addr_range(pgoff
, len
))
256 return (unsigned long) -EINVAL
;
257 return pgoff
<< PAGE_SHIFT
;
260 /* can't do an in-place private mapping if there's no MMU */
261 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
263 return vma
->vm_flags
& VM_MAYSHARE
;
266 #define get_unmapped_area_mem NULL
268 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
274 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
276 size_t size
= vma
->vm_end
- vma
->vm_start
;
278 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
281 if (!private_mapping_ok(vma
))
284 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
288 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
289 if (remap_pfn_range(vma
,
298 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
302 /* Turn a kernel-virtual address into a physical page frame */
303 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
306 * RED-PEN: on some architectures there is more mapped memory
307 * than available in mem_map which pfn_valid checks
308 * for. Perhaps should add a new macro here.
310 * RED-PEN: vmalloc is not supported right now.
316 return mmap_mem(file
, vma
);
319 #ifdef CONFIG_CRASH_DUMP
321 * Read memory corresponding to the old kernel.
323 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
324 size_t count
, loff_t
*ppos
)
326 unsigned long pfn
, offset
;
327 size_t read
= 0, csize
;
331 pfn
= *ppos
/ PAGE_SIZE
;
332 if (pfn
> saved_max_pfn
)
335 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
336 if (count
> PAGE_SIZE
- offset
)
337 csize
= PAGE_SIZE
- offset
;
341 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
353 extern long vread(char *buf
, char *addr
, unsigned long count
);
354 extern long vwrite(char *buf
, char *addr
, unsigned long count
);
357 * This function reads the *virtual* memory as seen by the kernel.
359 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
360 size_t count
, loff_t
*ppos
)
362 unsigned long p
= *ppos
;
363 ssize_t low_count
, read
, sz
;
364 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
367 if (p
< (unsigned long) high_memory
) {
369 if (count
> (unsigned long) high_memory
- p
)
370 low_count
= (unsigned long) high_memory
- p
;
372 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
373 /* we don't have page 0 mapped on sparc and m68k.. */
374 if (p
< PAGE_SIZE
&& low_count
> 0) {
375 size_t tmp
= PAGE_SIZE
- p
;
376 if (tmp
> low_count
) tmp
= low_count
;
377 if (clear_user(buf
, tmp
))
386 while (low_count
> 0) {
388 * Handle first page in case it's not aligned
390 if (-p
& (PAGE_SIZE
- 1))
391 sz
= -p
& (PAGE_SIZE
- 1);
395 sz
= min_t(unsigned long, sz
, low_count
);
398 * On ia64 if a page has been mapped somewhere as
399 * uncached, then it must also be accessed uncached
400 * by the kernel or data corruption may occur
402 kbuf
= xlate_dev_kmem_ptr((char *)p
);
404 if (copy_to_user(buf
, kbuf
, sz
))
415 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
423 len
= vread(kbuf
, (char *)p
, len
);
426 if (copy_to_user(buf
, kbuf
, len
)) {
427 free_page((unsigned long)kbuf
);
435 free_page((unsigned long)kbuf
);
442 static inline ssize_t
443 do_write_kmem(void *p
, unsigned long realp
, const char __user
* buf
,
444 size_t count
, loff_t
*ppos
)
447 unsigned long copied
;
450 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
451 /* we don't have page 0 mapped on sparc and m68k.. */
452 if (realp
< PAGE_SIZE
) {
453 unsigned long sz
= PAGE_SIZE
- realp
;
456 /* Hmm. Do something? */
468 * Handle first page in case it's not aligned
470 if (-realp
& (PAGE_SIZE
- 1))
471 sz
= -realp
& (PAGE_SIZE
- 1);
475 sz
= min_t(unsigned long, sz
, count
);
478 * On ia64 if a page has been mapped somewhere as
479 * uncached, then it must also be accessed uncached
480 * by the kernel or data corruption may occur
482 ptr
= xlate_dev_kmem_ptr(p
);
484 copied
= copy_from_user(ptr
, buf
, sz
);
486 written
+= sz
- copied
;
504 * This function writes to the *virtual* memory as seen by the kernel.
506 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
507 size_t count
, loff_t
*ppos
)
509 unsigned long p
= *ppos
;
513 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
515 if (p
< (unsigned long) high_memory
) {
518 if (count
> (unsigned long) high_memory
- p
)
519 wrote
= (unsigned long) high_memory
- p
;
521 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
522 if (written
!= wrote
)
531 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
533 return wrote
? wrote
: -ENOMEM
;
540 written
= copy_from_user(kbuf
, buf
, len
);
544 free_page((unsigned long)kbuf
);
548 len
= vwrite(kbuf
, (char *)p
, len
);
554 free_page((unsigned long)kbuf
);
558 return virtr
+ wrote
;
561 #ifdef CONFIG_DEVPORT
562 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
563 size_t count
, loff_t
*ppos
)
565 unsigned long i
= *ppos
;
566 char __user
*tmp
= buf
;
568 if (!access_ok(VERIFY_WRITE
, buf
, count
))
570 while (count
-- > 0 && i
< 65536) {
571 if (__put_user(inb(i
),tmp
) < 0)
580 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
581 size_t count
, loff_t
*ppos
)
583 unsigned long i
= *ppos
;
584 const char __user
* tmp
= buf
;
586 if (!access_ok(VERIFY_READ
,buf
,count
))
588 while (count
-- > 0 && i
< 65536) {
590 if (__get_user(c
, tmp
)) {
604 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
605 size_t count
, loff_t
*ppos
)
610 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
611 size_t count
, loff_t
*ppos
)
616 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
617 struct splice_desc
*sd
)
622 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
623 loff_t
*ppos
, size_t len
, unsigned int flags
)
625 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
630 * For fun, we are using the MMU for this.
632 static inline size_t read_zero_pagealigned(char __user
* buf
, size_t size
)
634 struct mm_struct
*mm
;
635 struct vm_area_struct
* vma
;
636 unsigned long addr
=(unsigned long)buf
;
639 /* Oops, this was forgotten before. -ben */
640 down_read(&mm
->mmap_sem
);
642 /* For private mappings, just map in zero pages. */
643 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
646 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
648 if (vma
->vm_flags
& (VM_SHARED
| VM_HUGETLB
))
650 count
= vma
->vm_end
- addr
;
654 zap_page_range(vma
, addr
, count
, NULL
);
655 if (zeromap_page_range(vma
, addr
, count
, PAGE_COPY
))
665 up_read(&mm
->mmap_sem
);
667 /* The shared case is hard. Let's do the conventional zeroing. */
669 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
671 return size
+ unwritten
- PAGE_SIZE
;
679 up_read(&mm
->mmap_sem
);
683 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
684 size_t count
, loff_t
*ppos
)
686 unsigned long left
, unwritten
, written
= 0;
691 if (!access_ok(VERIFY_WRITE
, buf
, count
))
696 /* do we want to be clever? Arbitrary cut-off */
697 if (count
>= PAGE_SIZE
*4) {
698 unsigned long partial
;
700 /* How much left of the page? */
701 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
702 unwritten
= clear_user(buf
, partial
);
703 written
= partial
- unwritten
;
708 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
709 written
+= (left
& PAGE_MASK
) - unwritten
;
712 buf
+= left
& PAGE_MASK
;
715 unwritten
= clear_user(buf
, left
);
716 written
+= left
- unwritten
;
718 return written
? written
: -EFAULT
;
721 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
725 if (vma
->vm_flags
& VM_SHARED
)
726 return shmem_zero_setup(vma
);
727 err
= zeromap_page_range(vma
, vma
->vm_start
,
728 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
729 BUG_ON(err
== -EEXIST
);
732 #else /* CONFIG_MMU */
733 static ssize_t
read_zero(struct file
* file
, char * buf
,
734 size_t count
, loff_t
*ppos
)
742 chunk
= 4096; /* Just for latency reasons */
743 if (clear_user(buf
, chunk
))
752 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
756 #endif /* CONFIG_MMU */
758 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
759 size_t count
, loff_t
*ppos
)
765 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
766 * can fopen() both devices with "a" now. This was previously impossible.
770 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
772 return file
->f_pos
= 0;
776 * The memory devices use the full 32/64 bits of the offset, and so we cannot
777 * check against negative addresses: they are ok. The return value is weird,
778 * though, in that case (0).
780 * also note that seeking relative to the "end of file" isn't supported:
781 * it has no meaning, so it returns -EINVAL.
783 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
787 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
790 file
->f_pos
= offset
;
792 force_successful_syscall_return();
795 file
->f_pos
+= offset
;
797 force_successful_syscall_return();
802 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
806 static int open_port(struct inode
* inode
, struct file
* filp
)
808 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
811 #define zero_lseek null_lseek
812 #define full_lseek null_lseek
813 #define write_zero write_null
814 #define read_full read_zero
815 #define open_mem open_port
816 #define open_kmem open_mem
817 #define open_oldmem open_mem
819 static const struct file_operations mem_fops
= {
820 .llseek
= memory_lseek
,
825 .get_unmapped_area
= get_unmapped_area_mem
,
828 static const struct file_operations kmem_fops
= {
829 .llseek
= memory_lseek
,
834 .get_unmapped_area
= get_unmapped_area_mem
,
837 static const struct file_operations null_fops
= {
838 .llseek
= null_lseek
,
841 .splice_write
= splice_write_null
,
844 #ifdef CONFIG_DEVPORT
845 static const struct file_operations port_fops
= {
846 .llseek
= memory_lseek
,
853 static const struct file_operations zero_fops
= {
854 .llseek
= zero_lseek
,
861 * capabilities for /dev/zero
862 * - permits private mappings, "copies" are taken of the source of zeros
864 static struct backing_dev_info zero_bdi
= {
865 .capabilities
= BDI_CAP_MAP_COPY
,
868 static const struct file_operations full_fops
= {
869 .llseek
= full_lseek
,
874 #ifdef CONFIG_CRASH_DUMP
875 static const struct file_operations oldmem_fops
= {
881 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
882 size_t count
, loff_t
*ppos
)
887 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
891 if (!copy_from_user(tmp
, buf
, count
)) {
893 ret
= printk("%s", tmp
);
895 /* printk can add a prefix */
902 static const struct file_operations kmsg_fops
= {
906 static int memory_open(struct inode
* inode
, struct file
* filp
)
908 switch (iminor(inode
)) {
910 filp
->f_op
= &mem_fops
;
911 filp
->f_mapping
->backing_dev_info
=
912 &directly_mappable_cdev_bdi
;
915 filp
->f_op
= &kmem_fops
;
916 filp
->f_mapping
->backing_dev_info
=
917 &directly_mappable_cdev_bdi
;
920 filp
->f_op
= &null_fops
;
922 #ifdef CONFIG_DEVPORT
924 filp
->f_op
= &port_fops
;
928 filp
->f_mapping
->backing_dev_info
= &zero_bdi
;
929 filp
->f_op
= &zero_fops
;
932 filp
->f_op
= &full_fops
;
935 filp
->f_op
= &random_fops
;
938 filp
->f_op
= &urandom_fops
;
941 filp
->f_op
= &kmsg_fops
;
943 #ifdef CONFIG_CRASH_DUMP
945 filp
->f_op
= &oldmem_fops
;
951 if (filp
->f_op
&& filp
->f_op
->open
)
952 return filp
->f_op
->open(inode
,filp
);
956 static const struct file_operations memory_fops
= {
957 .open
= memory_open
, /* just a selector for the real open */
960 static const struct {
964 const struct file_operations
*fops
;
965 } devlist
[] = { /* list of minor devices */
966 {1, "mem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &mem_fops
},
967 {2, "kmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &kmem_fops
},
968 {3, "null", S_IRUGO
| S_IWUGO
, &null_fops
},
969 #ifdef CONFIG_DEVPORT
970 {4, "port", S_IRUSR
| S_IWUSR
| S_IRGRP
, &port_fops
},
972 {5, "zero", S_IRUGO
| S_IWUGO
, &zero_fops
},
973 {7, "full", S_IRUGO
| S_IWUGO
, &full_fops
},
974 {8, "random", S_IRUGO
| S_IWUSR
, &random_fops
},
975 {9, "urandom", S_IRUGO
| S_IWUSR
, &urandom_fops
},
976 {11,"kmsg", S_IRUGO
| S_IWUSR
, &kmsg_fops
},
977 #ifdef CONFIG_CRASH_DUMP
978 {12,"oldmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &oldmem_fops
},
982 static struct class *mem_class
;
984 static int __init
chr_dev_init(void)
988 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
989 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
991 mem_class
= class_create(THIS_MODULE
, "mem");
992 for (i
= 0; i
< ARRAY_SIZE(devlist
); i
++)
993 device_create(mem_class
, NULL
,
994 MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
1000 fs_initcall(chr_dev_init
);