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/smp_lock.h>
22 #include <linux/ptrace.h>
23 #include <linux/device.h>
24 #include <linux/highmem.h>
25 #include <linux/crash_dump.h>
26 #include <linux/backing-dev.h>
27 #include <linux/bootmem.h>
28 #include <linux/pipe_fs_i.h>
29 #include <linux/pfn.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 pfn
, 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
);
243 static unsigned long get_unmapped_area_mem(struct file
*file
,
249 if (!valid_mmap_phys_addr_range(pgoff
, len
))
250 return (unsigned long) -EINVAL
;
254 /* can't do an in-place private mapping if there's no MMU */
255 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
257 return vma
->vm_flags
& VM_MAYSHARE
;
260 #define get_unmapped_area_mem NULL
262 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
268 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
270 size_t size
= vma
->vm_end
- vma
->vm_start
;
272 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
275 if (!private_mapping_ok(vma
))
278 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
282 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
283 if (remap_pfn_range(vma
,
292 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
296 /* Turn a pfn offset into an absolute pfn */
297 pfn
= PFN_DOWN(virt_to_phys((void *)PAGE_OFFSET
)) + vma
->vm_pgoff
;
300 * RED-PEN: on some architectures there is more mapped memory
301 * than available in mem_map which pfn_valid checks
302 * for. Perhaps should add a new macro here.
304 * RED-PEN: vmalloc is not supported right now.
310 return mmap_mem(file
, vma
);
313 #ifdef CONFIG_CRASH_DUMP
315 * Read memory corresponding to the old kernel.
317 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
318 size_t count
, loff_t
*ppos
)
320 unsigned long pfn
, offset
;
321 size_t read
= 0, csize
;
325 pfn
= *ppos
/ PAGE_SIZE
;
326 if (pfn
> saved_max_pfn
)
329 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
330 if (count
> PAGE_SIZE
- offset
)
331 csize
= PAGE_SIZE
- offset
;
335 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
347 extern long vread(char *buf
, char *addr
, unsigned long count
);
348 extern long vwrite(char *buf
, char *addr
, unsigned long count
);
351 * This function reads the *virtual* memory as seen by the kernel.
353 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
354 size_t count
, loff_t
*ppos
)
356 unsigned long p
= *ppos
;
357 ssize_t low_count
, read
, sz
;
358 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
361 if (p
< (unsigned long) high_memory
) {
363 if (count
> (unsigned long) high_memory
- p
)
364 low_count
= (unsigned long) high_memory
- p
;
366 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
367 /* we don't have page 0 mapped on sparc and m68k.. */
368 if (p
< PAGE_SIZE
&& low_count
> 0) {
369 size_t tmp
= PAGE_SIZE
- p
;
370 if (tmp
> low_count
) tmp
= low_count
;
371 if (clear_user(buf
, tmp
))
380 while (low_count
> 0) {
382 * Handle first page in case it's not aligned
384 if (-p
& (PAGE_SIZE
- 1))
385 sz
= -p
& (PAGE_SIZE
- 1);
389 sz
= min_t(unsigned long, sz
, low_count
);
392 * On ia64 if a page has been mapped somewhere as
393 * uncached, then it must also be accessed uncached
394 * by the kernel or data corruption may occur
396 kbuf
= xlate_dev_kmem_ptr((char *)p
);
398 if (copy_to_user(buf
, kbuf
, sz
))
409 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
417 len
= vread(kbuf
, (char *)p
, len
);
420 if (copy_to_user(buf
, kbuf
, len
)) {
421 free_page((unsigned long)kbuf
);
429 free_page((unsigned long)kbuf
);
436 static inline ssize_t
437 do_write_kmem(void *p
, unsigned long realp
, const char __user
* buf
,
438 size_t count
, loff_t
*ppos
)
441 unsigned long copied
;
444 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
445 /* we don't have page 0 mapped on sparc and m68k.. */
446 if (realp
< PAGE_SIZE
) {
447 unsigned long sz
= PAGE_SIZE
- realp
;
450 /* Hmm. Do something? */
462 * Handle first page in case it's not aligned
464 if (-realp
& (PAGE_SIZE
- 1))
465 sz
= -realp
& (PAGE_SIZE
- 1);
469 sz
= min_t(unsigned long, sz
, count
);
472 * On ia64 if a page has been mapped somewhere as
473 * uncached, then it must also be accessed uncached
474 * by the kernel or data corruption may occur
476 ptr
= xlate_dev_kmem_ptr(p
);
478 copied
= copy_from_user(ptr
, buf
, sz
);
480 written
+= sz
- copied
;
498 * This function writes to the *virtual* memory as seen by the kernel.
500 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
501 size_t count
, loff_t
*ppos
)
503 unsigned long p
= *ppos
;
507 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
509 if (p
< (unsigned long) high_memory
) {
512 if (count
> (unsigned long) high_memory
- p
)
513 wrote
= (unsigned long) high_memory
- p
;
515 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
516 if (written
!= wrote
)
525 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
527 return wrote
? wrote
: -ENOMEM
;
534 written
= copy_from_user(kbuf
, buf
, len
);
538 free_page((unsigned long)kbuf
);
542 len
= vwrite(kbuf
, (char *)p
, len
);
548 free_page((unsigned long)kbuf
);
552 return virtr
+ wrote
;
555 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
556 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
557 size_t count
, loff_t
*ppos
)
559 unsigned long i
= *ppos
;
560 char __user
*tmp
= buf
;
562 if (!access_ok(VERIFY_WRITE
, buf
, count
))
564 while (count
-- > 0 && i
< 65536) {
565 if (__put_user(inb(i
),tmp
) < 0)
574 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
575 size_t count
, loff_t
*ppos
)
577 unsigned long i
= *ppos
;
578 const char __user
* tmp
= buf
;
580 if (!access_ok(VERIFY_READ
,buf
,count
))
582 while (count
-- > 0 && i
< 65536) {
584 if (__get_user(c
, tmp
)) {
598 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
599 size_t count
, loff_t
*ppos
)
604 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
605 size_t count
, loff_t
*ppos
)
610 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
611 struct splice_desc
*sd
)
616 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
617 loff_t
*ppos
, size_t len
, unsigned int flags
)
619 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
624 * For fun, we are using the MMU for this.
626 static inline size_t read_zero_pagealigned(char __user
* buf
, size_t size
)
628 struct mm_struct
*mm
;
629 struct vm_area_struct
* vma
;
630 unsigned long addr
=(unsigned long)buf
;
633 /* Oops, this was forgotten before. -ben */
634 down_read(&mm
->mmap_sem
);
636 /* For private mappings, just map in zero pages. */
637 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
640 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
642 if (vma
->vm_flags
& (VM_SHARED
| VM_HUGETLB
))
644 count
= vma
->vm_end
- addr
;
648 zap_page_range(vma
, addr
, count
, NULL
);
649 if (zeromap_page_range(vma
, addr
, count
, PAGE_COPY
))
659 up_read(&mm
->mmap_sem
);
661 /* The shared case is hard. Let's do the conventional zeroing. */
663 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
665 return size
+ unwritten
- PAGE_SIZE
;
673 up_read(&mm
->mmap_sem
);
677 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
678 size_t count
, loff_t
*ppos
)
680 unsigned long left
, unwritten
, written
= 0;
685 if (!access_ok(VERIFY_WRITE
, buf
, count
))
690 /* do we want to be clever? Arbitrary cut-off */
691 if (count
>= PAGE_SIZE
*4) {
692 unsigned long partial
;
694 /* How much left of the page? */
695 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
696 unwritten
= clear_user(buf
, partial
);
697 written
= partial
- unwritten
;
702 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
703 written
+= (left
& PAGE_MASK
) - unwritten
;
706 buf
+= left
& PAGE_MASK
;
709 unwritten
= clear_user(buf
, left
);
710 written
+= left
- unwritten
;
712 return written
? written
: -EFAULT
;
715 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
719 if (vma
->vm_flags
& VM_SHARED
)
720 return shmem_zero_setup(vma
);
721 err
= zeromap_page_range(vma
, vma
->vm_start
,
722 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
723 BUG_ON(err
== -EEXIST
);
726 #else /* CONFIG_MMU */
727 static ssize_t
read_zero(struct file
* file
, char * buf
,
728 size_t count
, loff_t
*ppos
)
736 chunk
= 4096; /* Just for latency reasons */
737 if (clear_user(buf
, chunk
))
746 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
750 #endif /* CONFIG_MMU */
752 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
753 size_t count
, loff_t
*ppos
)
759 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
760 * can fopen() both devices with "a" now. This was previously impossible.
764 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
766 return file
->f_pos
= 0;
770 * The memory devices use the full 32/64 bits of the offset, and so we cannot
771 * check against negative addresses: they are ok. The return value is weird,
772 * though, in that case (0).
774 * also note that seeking relative to the "end of file" isn't supported:
775 * it has no meaning, so it returns -EINVAL.
777 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
781 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
784 file
->f_pos
= offset
;
786 force_successful_syscall_return();
789 file
->f_pos
+= offset
;
791 force_successful_syscall_return();
796 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
800 static int open_port(struct inode
* inode
, struct file
* filp
)
802 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
805 #define zero_lseek null_lseek
806 #define full_lseek null_lseek
807 #define write_zero write_null
808 #define read_full read_zero
809 #define open_mem open_port
810 #define open_kmem open_mem
811 #define open_oldmem open_mem
813 static const struct file_operations mem_fops
= {
814 .llseek
= memory_lseek
,
819 .get_unmapped_area
= get_unmapped_area_mem
,
822 static const struct file_operations kmem_fops
= {
823 .llseek
= memory_lseek
,
828 .get_unmapped_area
= get_unmapped_area_mem
,
831 static const struct file_operations null_fops
= {
832 .llseek
= null_lseek
,
835 .splice_write
= splice_write_null
,
838 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
839 static const struct file_operations port_fops
= {
840 .llseek
= memory_lseek
,
847 static const struct file_operations zero_fops
= {
848 .llseek
= zero_lseek
,
855 * capabilities for /dev/zero
856 * - permits private mappings, "copies" are taken of the source of zeros
858 static struct backing_dev_info zero_bdi
= {
859 .capabilities
= BDI_CAP_MAP_COPY
,
862 static const struct file_operations full_fops
= {
863 .llseek
= full_lseek
,
868 #ifdef CONFIG_CRASH_DUMP
869 static const struct file_operations oldmem_fops
= {
875 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
876 size_t count
, loff_t
*ppos
)
881 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
885 if (!copy_from_user(tmp
, buf
, count
)) {
887 ret
= printk("%s", tmp
);
889 /* printk can add a prefix */
896 static const struct file_operations kmsg_fops
= {
900 static int memory_open(struct inode
* inode
, struct file
* filp
)
902 switch (iminor(inode
)) {
904 filp
->f_op
= &mem_fops
;
905 filp
->f_mapping
->backing_dev_info
=
906 &directly_mappable_cdev_bdi
;
909 filp
->f_op
= &kmem_fops
;
910 filp
->f_mapping
->backing_dev_info
=
911 &directly_mappable_cdev_bdi
;
914 filp
->f_op
= &null_fops
;
916 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
918 filp
->f_op
= &port_fops
;
922 filp
->f_mapping
->backing_dev_info
= &zero_bdi
;
923 filp
->f_op
= &zero_fops
;
926 filp
->f_op
= &full_fops
;
929 filp
->f_op
= &random_fops
;
932 filp
->f_op
= &urandom_fops
;
935 filp
->f_op
= &kmsg_fops
;
937 #ifdef CONFIG_CRASH_DUMP
939 filp
->f_op
= &oldmem_fops
;
945 if (filp
->f_op
&& filp
->f_op
->open
)
946 return filp
->f_op
->open(inode
,filp
);
950 static const struct file_operations memory_fops
= {
951 .open
= memory_open
, /* just a selector for the real open */
954 static const struct {
958 const struct file_operations
*fops
;
959 } devlist
[] = { /* list of minor devices */
960 {1, "mem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &mem_fops
},
961 {2, "kmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &kmem_fops
},
962 {3, "null", S_IRUGO
| S_IWUGO
, &null_fops
},
963 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
964 {4, "port", S_IRUSR
| S_IWUSR
| S_IRGRP
, &port_fops
},
966 {5, "zero", S_IRUGO
| S_IWUGO
, &zero_fops
},
967 {7, "full", S_IRUGO
| S_IWUGO
, &full_fops
},
968 {8, "random", S_IRUGO
| S_IWUSR
, &random_fops
},
969 {9, "urandom", S_IRUGO
| S_IWUSR
, &urandom_fops
},
970 {11,"kmsg", S_IRUGO
| S_IWUSR
, &kmsg_fops
},
971 #ifdef CONFIG_CRASH_DUMP
972 {12,"oldmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &oldmem_fops
},
976 static struct class *mem_class
;
978 static int __init
chr_dev_init(void)
982 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
983 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
985 mem_class
= class_create(THIS_MODULE
, "mem");
986 for (i
= 0; i
< ARRAY_SIZE(devlist
); i
++)
987 device_create(mem_class
, NULL
,
988 MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
994 fs_initcall(chr_dev_init
);