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>
40 * Architectures vary in how they handle caching for addresses
41 * outside of main memory.
44 static inline int uncached_access(struct file
*file
, unsigned long addr
)
48 * On the PPro and successors, the MTRRs are used to set
49 * memory types for physical addresses outside main memory,
50 * so blindly setting PCD or PWT on those pages is wrong.
51 * For Pentiums and earlier, the surround logic should disable
52 * caching for the high addresses through the KEN pin, but
53 * we maintain the tradition of paranoia in this code.
55 if (file
->f_flags
& O_SYNC
)
57 return !( test_bit(X86_FEATURE_MTRR
, boot_cpu_data
.x86_capability
) ||
58 test_bit(X86_FEATURE_K6_MTRR
, boot_cpu_data
.x86_capability
) ||
59 test_bit(X86_FEATURE_CYRIX_ARR
, boot_cpu_data
.x86_capability
) ||
60 test_bit(X86_FEATURE_CENTAUR_MCR
, boot_cpu_data
.x86_capability
) )
61 && addr
>= __pa(high_memory
);
62 #elif defined(__x86_64__)
64 * This is broken because it can generate memory type aliases,
65 * which can cause cache corruptions
66 * But it is only available for root and we have to be bug-to-bug
67 * compatible with i386.
69 if (file
->f_flags
& O_SYNC
)
71 /* same behaviour as i386. PAT always set to cached and MTRRs control the
73 Hopefully a full PAT implementation will fix that soon. */
75 #elif defined(CONFIG_IA64)
77 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
79 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
82 * Accessing memory above the top the kernel knows about or through a file pointer
83 * that was marked O_SYNC will be done non-cached.
85 if (file
->f_flags
& O_SYNC
)
87 return addr
>= __pa(high_memory
);
91 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
92 static inline int valid_phys_addr_range(unsigned long addr
, size_t count
)
94 if (addr
+ count
> __pa(high_memory
))
100 static inline int valid_mmap_phys_addr_range(unsigned long addr
, size_t size
)
107 * This funcion reads the *physical* memory. The f_pos points directly to the
110 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
111 size_t count
, loff_t
*ppos
)
113 unsigned long p
= *ppos
;
117 if (!valid_phys_addr_range(p
, count
))
120 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
121 /* we don't have page 0 mapped on sparc and m68k.. */
127 if (clear_user(buf
, sz
))
139 * Handle first page in case it's not aligned
141 if (-p
& (PAGE_SIZE
- 1))
142 sz
= -p
& (PAGE_SIZE
- 1);
146 sz
= min_t(unsigned long, sz
, count
);
149 * On ia64 if a page has been mapped somewhere as
150 * uncached, then it must also be accessed uncached
151 * by the kernel or data corruption may occur
153 ptr
= xlate_dev_mem_ptr(p
);
155 if (copy_to_user(buf
, ptr
, sz
))
167 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
168 size_t count
, loff_t
*ppos
)
170 unsigned long p
= *ppos
;
172 unsigned long copied
;
175 if (!valid_phys_addr_range(p
, count
))
180 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
181 /* we don't have page 0 mapped on sparc and m68k.. */
183 unsigned long sz
= PAGE_SIZE
- p
;
186 /* Hmm. Do something? */
196 * Handle first page in case it's not aligned
198 if (-p
& (PAGE_SIZE
- 1))
199 sz
= -p
& (PAGE_SIZE
- 1);
203 sz
= min_t(unsigned long, sz
, count
);
206 * On ia64 if a page has been mapped somewhere as
207 * uncached, then it must also be accessed uncached
208 * by the kernel or data corruption may occur
210 ptr
= xlate_dev_mem_ptr(p
);
212 copied
= copy_from_user(ptr
, buf
, sz
);
214 written
+= sz
- copied
;
229 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
230 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
231 unsigned long size
, pgprot_t vma_prot
)
233 #ifdef pgprot_noncached
234 unsigned long offset
= pfn
<< PAGE_SHIFT
;
236 if (uncached_access(file
, offset
))
237 return pgprot_noncached(vma_prot
);
243 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
245 size_t size
= vma
->vm_end
- vma
->vm_start
;
247 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
<< PAGE_SHIFT
, size
))
250 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
254 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
255 if (remap_pfn_range(vma
,
264 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
268 /* Turn a kernel-virtual address into a physical page frame */
269 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
272 * RED-PEN: on some architectures there is more mapped memory
273 * than available in mem_map which pfn_valid checks
274 * for. Perhaps should add a new macro here.
276 * RED-PEN: vmalloc is not supported right now.
282 return mmap_mem(file
, vma
);
285 #ifdef CONFIG_CRASH_DUMP
287 * Read memory corresponding to the old kernel.
289 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
290 size_t count
, loff_t
*ppos
)
292 unsigned long pfn
, offset
;
293 size_t read
= 0, csize
;
297 pfn
= *ppos
/ PAGE_SIZE
;
298 if (pfn
> saved_max_pfn
)
301 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
302 if (count
> PAGE_SIZE
- offset
)
303 csize
= PAGE_SIZE
- offset
;
307 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
319 extern long vread(char *buf
, char *addr
, unsigned long count
);
320 extern long vwrite(char *buf
, char *addr
, unsigned long count
);
323 * This function reads the *virtual* memory as seen by the kernel.
325 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
326 size_t count
, loff_t
*ppos
)
328 unsigned long p
= *ppos
;
329 ssize_t low_count
, read
, sz
;
330 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
333 if (p
< (unsigned long) high_memory
) {
335 if (count
> (unsigned long) high_memory
- p
)
336 low_count
= (unsigned long) high_memory
- p
;
338 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
339 /* we don't have page 0 mapped on sparc and m68k.. */
340 if (p
< PAGE_SIZE
&& low_count
> 0) {
341 size_t tmp
= PAGE_SIZE
- p
;
342 if (tmp
> low_count
) tmp
= low_count
;
343 if (clear_user(buf
, tmp
))
352 while (low_count
> 0) {
354 * Handle first page in case it's not aligned
356 if (-p
& (PAGE_SIZE
- 1))
357 sz
= -p
& (PAGE_SIZE
- 1);
361 sz
= min_t(unsigned long, sz
, low_count
);
364 * On ia64 if a page has been mapped somewhere as
365 * uncached, then it must also be accessed uncached
366 * by the kernel or data corruption may occur
368 kbuf
= xlate_dev_kmem_ptr((char *)p
);
370 if (copy_to_user(buf
, kbuf
, sz
))
381 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
389 len
= vread(kbuf
, (char *)p
, len
);
392 if (copy_to_user(buf
, kbuf
, len
)) {
393 free_page((unsigned long)kbuf
);
401 free_page((unsigned long)kbuf
);
408 static inline ssize_t
409 do_write_kmem(void *p
, unsigned long realp
, const char __user
* buf
,
410 size_t count
, loff_t
*ppos
)
413 unsigned long copied
;
416 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
417 /* we don't have page 0 mapped on sparc and m68k.. */
418 if (realp
< PAGE_SIZE
) {
419 unsigned long sz
= PAGE_SIZE
- realp
;
422 /* Hmm. Do something? */
434 * Handle first page in case it's not aligned
436 if (-realp
& (PAGE_SIZE
- 1))
437 sz
= -realp
& (PAGE_SIZE
- 1);
441 sz
= min_t(unsigned long, sz
, count
);
444 * On ia64 if a page has been mapped somewhere as
445 * uncached, then it must also be accessed uncached
446 * by the kernel or data corruption may occur
448 ptr
= xlate_dev_kmem_ptr(p
);
450 copied
= copy_from_user(ptr
, buf
, sz
);
452 written
+= sz
- copied
;
470 * This function writes to the *virtual* memory as seen by the kernel.
472 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
473 size_t count
, loff_t
*ppos
)
475 unsigned long p
= *ppos
;
479 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
481 if (p
< (unsigned long) high_memory
) {
484 if (count
> (unsigned long) high_memory
- p
)
485 wrote
= (unsigned long) high_memory
- p
;
487 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
488 if (written
!= wrote
)
497 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
499 return wrote
? wrote
: -ENOMEM
;
506 written
= copy_from_user(kbuf
, buf
, len
);
510 free_page((unsigned long)kbuf
);
514 len
= vwrite(kbuf
, (char *)p
, len
);
520 free_page((unsigned long)kbuf
);
524 return virtr
+ wrote
;
527 #if defined(CONFIG_ISA) || !defined(__mc68000__)
528 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
529 size_t count
, loff_t
*ppos
)
531 unsigned long i
= *ppos
;
532 char __user
*tmp
= buf
;
534 if (!access_ok(VERIFY_WRITE
, buf
, count
))
536 while (count
-- > 0 && i
< 65536) {
537 if (__put_user(inb(i
),tmp
) < 0)
546 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
547 size_t count
, loff_t
*ppos
)
549 unsigned long i
= *ppos
;
550 const char __user
* tmp
= buf
;
552 if (!access_ok(VERIFY_READ
,buf
,count
))
554 while (count
-- > 0 && i
< 65536) {
556 if (__get_user(c
, tmp
)) {
570 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
571 size_t count
, loff_t
*ppos
)
576 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
577 size_t count
, loff_t
*ppos
)
582 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
583 struct splice_desc
*sd
)
588 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
589 loff_t
*ppos
, size_t len
, unsigned int flags
)
591 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
596 * For fun, we are using the MMU for this.
598 static inline size_t read_zero_pagealigned(char __user
* buf
, size_t size
)
600 struct mm_struct
*mm
;
601 struct vm_area_struct
* vma
;
602 unsigned long addr
=(unsigned long)buf
;
605 /* Oops, this was forgotten before. -ben */
606 down_read(&mm
->mmap_sem
);
608 /* For private mappings, just map in zero pages. */
609 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
612 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
614 if (vma
->vm_flags
& (VM_SHARED
| VM_HUGETLB
))
616 count
= vma
->vm_end
- addr
;
620 zap_page_range(vma
, addr
, count
, NULL
);
621 zeromap_page_range(vma
, addr
, count
, PAGE_COPY
);
630 up_read(&mm
->mmap_sem
);
632 /* The shared case is hard. Let's do the conventional zeroing. */
634 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
636 return size
+ unwritten
- PAGE_SIZE
;
644 up_read(&mm
->mmap_sem
);
648 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
649 size_t count
, loff_t
*ppos
)
651 unsigned long left
, unwritten
, written
= 0;
656 if (!access_ok(VERIFY_WRITE
, buf
, count
))
661 /* do we want to be clever? Arbitrary cut-off */
662 if (count
>= PAGE_SIZE
*4) {
663 unsigned long partial
;
665 /* How much left of the page? */
666 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
667 unwritten
= clear_user(buf
, partial
);
668 written
= partial
- unwritten
;
673 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
674 written
+= (left
& PAGE_MASK
) - unwritten
;
677 buf
+= left
& PAGE_MASK
;
680 unwritten
= clear_user(buf
, left
);
681 written
+= left
- unwritten
;
683 return written
? written
: -EFAULT
;
686 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
688 if (vma
->vm_flags
& VM_SHARED
)
689 return shmem_zero_setup(vma
);
690 if (zeromap_page_range(vma
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
694 #else /* CONFIG_MMU */
695 static ssize_t
read_zero(struct file
* file
, char * buf
,
696 size_t count
, loff_t
*ppos
)
704 chunk
= 4096; /* Just for latency reasons */
705 if (clear_user(buf
, chunk
))
714 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
718 #endif /* CONFIG_MMU */
720 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
721 size_t count
, loff_t
*ppos
)
727 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
728 * can fopen() both devices with "a" now. This was previously impossible.
732 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
734 return file
->f_pos
= 0;
738 * The memory devices use the full 32/64 bits of the offset, and so we cannot
739 * check against negative addresses: they are ok. The return value is weird,
740 * though, in that case (0).
742 * also note that seeking relative to the "end of file" isn't supported:
743 * it has no meaning, so it returns -EINVAL.
745 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
749 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
752 file
->f_pos
= offset
;
754 force_successful_syscall_return();
757 file
->f_pos
+= offset
;
759 force_successful_syscall_return();
764 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
768 static int open_port(struct inode
* inode
, struct file
* filp
)
770 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
773 #define zero_lseek null_lseek
774 #define full_lseek null_lseek
775 #define write_zero write_null
776 #define read_full read_zero
777 #define open_mem open_port
778 #define open_kmem open_mem
779 #define open_oldmem open_mem
781 static struct file_operations mem_fops
= {
782 .llseek
= memory_lseek
,
789 static struct file_operations kmem_fops
= {
790 .llseek
= memory_lseek
,
797 static struct file_operations null_fops
= {
798 .llseek
= null_lseek
,
801 .splice_write
= splice_write_null
,
804 #if defined(CONFIG_ISA) || !defined(__mc68000__)
805 static struct file_operations port_fops
= {
806 .llseek
= memory_lseek
,
813 static struct file_operations zero_fops
= {
814 .llseek
= zero_lseek
,
820 static struct backing_dev_info zero_bdi
= {
821 .capabilities
= BDI_CAP_MAP_COPY
,
824 static struct file_operations full_fops
= {
825 .llseek
= full_lseek
,
830 #ifdef CONFIG_CRASH_DUMP
831 static struct file_operations oldmem_fops
= {
837 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
838 size_t count
, loff_t
*ppos
)
843 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
847 if (!copy_from_user(tmp
, buf
, count
)) {
849 ret
= printk("%s", tmp
);
851 /* printk can add a prefix */
858 static struct file_operations kmsg_fops
= {
862 static int memory_open(struct inode
* inode
, struct file
* filp
)
864 switch (iminor(inode
)) {
866 filp
->f_op
= &mem_fops
;
869 filp
->f_op
= &kmem_fops
;
872 filp
->f_op
= &null_fops
;
874 #if defined(CONFIG_ISA) || !defined(__mc68000__)
876 filp
->f_op
= &port_fops
;
880 filp
->f_mapping
->backing_dev_info
= &zero_bdi
;
881 filp
->f_op
= &zero_fops
;
884 filp
->f_op
= &full_fops
;
887 filp
->f_op
= &random_fops
;
890 filp
->f_op
= &urandom_fops
;
893 filp
->f_op
= &kmsg_fops
;
895 #ifdef CONFIG_CRASH_DUMP
897 filp
->f_op
= &oldmem_fops
;
903 if (filp
->f_op
&& filp
->f_op
->open
)
904 return filp
->f_op
->open(inode
,filp
);
908 static struct file_operations memory_fops
= {
909 .open
= memory_open
, /* just a selector for the real open */
912 static const struct {
916 const struct file_operations
*fops
;
917 } devlist
[] = { /* list of minor devices */
918 {1, "mem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &mem_fops
},
919 {2, "kmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &kmem_fops
},
920 {3, "null", S_IRUGO
| S_IWUGO
, &null_fops
},
921 #if defined(CONFIG_ISA) || !defined(__mc68000__)
922 {4, "port", S_IRUSR
| S_IWUSR
| S_IRGRP
, &port_fops
},
924 {5, "zero", S_IRUGO
| S_IWUGO
, &zero_fops
},
925 {7, "full", S_IRUGO
| S_IWUGO
, &full_fops
},
926 {8, "random", S_IRUGO
| S_IWUSR
, &random_fops
},
927 {9, "urandom", S_IRUGO
| S_IWUSR
, &urandom_fops
},
928 {11,"kmsg", S_IRUGO
| S_IWUSR
, &kmsg_fops
},
929 #ifdef CONFIG_CRASH_DUMP
930 {12,"oldmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &oldmem_fops
},
934 static struct class *mem_class
;
936 static int __init
chr_dev_init(void)
940 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
941 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
943 mem_class
= class_create(THIS_MODULE
, "mem");
944 for (i
= 0; i
< ARRAY_SIZE(devlist
); i
++) {
945 class_device_create(mem_class
, NULL
,
946 MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
947 NULL
, devlist
[i
].name
);
948 devfs_mk_cdev(MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
949 S_IFCHR
| devlist
[i
].mode
, devlist
[i
].name
);
955 fs_initcall(chr_dev_init
);