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>
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
);
80 * Accessing memory above the top the kernel knows about or through a file pointer
81 * that was marked O_SYNC will be done non-cached.
83 if (file
->f_flags
& O_SYNC
)
85 return addr
>= __pa(high_memory
);
89 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
90 static inline int valid_phys_addr_range(unsigned long addr
, size_t count
)
92 if (addr
+ count
> __pa(high_memory
))
98 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
105 * This funcion reads the *physical* memory. The f_pos points directly to the
108 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
109 size_t count
, loff_t
*ppos
)
111 unsigned long p
= *ppos
;
115 if (!valid_phys_addr_range(p
, count
))
118 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
119 /* we don't have page 0 mapped on sparc and m68k.. */
125 if (clear_user(buf
, sz
))
137 * Handle first page in case it's not aligned
139 if (-p
& (PAGE_SIZE
- 1))
140 sz
= -p
& (PAGE_SIZE
- 1);
144 sz
= min_t(unsigned long, sz
, count
);
147 * On ia64 if a page has been mapped somewhere as
148 * uncached, then it must also be accessed uncached
149 * by the kernel or data corruption may occur
151 ptr
= xlate_dev_mem_ptr(p
);
153 if (copy_to_user(buf
, ptr
, sz
))
165 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
166 size_t count
, loff_t
*ppos
)
168 unsigned long p
= *ppos
;
170 unsigned long copied
;
173 if (!valid_phys_addr_range(p
, count
))
178 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
179 /* we don't have page 0 mapped on sparc and m68k.. */
181 unsigned long sz
= PAGE_SIZE
- p
;
184 /* Hmm. Do something? */
194 * Handle first page in case it's not aligned
196 if (-p
& (PAGE_SIZE
- 1))
197 sz
= -p
& (PAGE_SIZE
- 1);
201 sz
= min_t(unsigned long, sz
, count
);
204 * On ia64 if a page has been mapped somewhere as
205 * uncached, then it must also be accessed uncached
206 * by the kernel or data corruption may occur
208 ptr
= xlate_dev_mem_ptr(p
);
210 copied
= copy_from_user(ptr
, buf
, sz
);
212 written
+= sz
- copied
;
227 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
228 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
229 unsigned long size
, pgprot_t vma_prot
)
231 #ifdef pgprot_noncached
232 unsigned long offset
= pfn
<< PAGE_SHIFT
;
234 if (uncached_access(file
, offset
))
235 return pgprot_noncached(vma_prot
);
241 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
243 size_t size
= vma
->vm_end
- vma
->vm_start
;
245 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
248 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
252 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
253 if (remap_pfn_range(vma
,
262 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
266 /* Turn a kernel-virtual address into a physical page frame */
267 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
270 * RED-PEN: on some architectures there is more mapped memory
271 * than available in mem_map which pfn_valid checks
272 * for. Perhaps should add a new macro here.
274 * RED-PEN: vmalloc is not supported right now.
280 return mmap_mem(file
, vma
);
283 #ifdef CONFIG_CRASH_DUMP
285 * Read memory corresponding to the old kernel.
287 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
288 size_t count
, loff_t
*ppos
)
290 unsigned long pfn
, offset
;
291 size_t read
= 0, csize
;
295 pfn
= *ppos
/ PAGE_SIZE
;
296 if (pfn
> saved_max_pfn
)
299 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
300 if (count
> PAGE_SIZE
- offset
)
301 csize
= PAGE_SIZE
- offset
;
305 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
317 extern long vread(char *buf
, char *addr
, unsigned long count
);
318 extern long vwrite(char *buf
, char *addr
, unsigned long count
);
321 * This function reads the *virtual* memory as seen by the kernel.
323 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
324 size_t count
, loff_t
*ppos
)
326 unsigned long p
= *ppos
;
327 ssize_t low_count
, read
, sz
;
328 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
331 if (p
< (unsigned long) high_memory
) {
333 if (count
> (unsigned long) high_memory
- p
)
334 low_count
= (unsigned long) high_memory
- p
;
336 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
337 /* we don't have page 0 mapped on sparc and m68k.. */
338 if (p
< PAGE_SIZE
&& low_count
> 0) {
339 size_t tmp
= PAGE_SIZE
- p
;
340 if (tmp
> low_count
) tmp
= low_count
;
341 if (clear_user(buf
, tmp
))
350 while (low_count
> 0) {
352 * Handle first page in case it's not aligned
354 if (-p
& (PAGE_SIZE
- 1))
355 sz
= -p
& (PAGE_SIZE
- 1);
359 sz
= min_t(unsigned long, sz
, low_count
);
362 * On ia64 if a page has been mapped somewhere as
363 * uncached, then it must also be accessed uncached
364 * by the kernel or data corruption may occur
366 kbuf
= xlate_dev_kmem_ptr((char *)p
);
368 if (copy_to_user(buf
, kbuf
, sz
))
379 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
387 len
= vread(kbuf
, (char *)p
, len
);
390 if (copy_to_user(buf
, kbuf
, len
)) {
391 free_page((unsigned long)kbuf
);
399 free_page((unsigned long)kbuf
);
406 static inline ssize_t
407 do_write_kmem(void *p
, unsigned long realp
, const char __user
* buf
,
408 size_t count
, loff_t
*ppos
)
411 unsigned long copied
;
414 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
415 /* we don't have page 0 mapped on sparc and m68k.. */
416 if (realp
< PAGE_SIZE
) {
417 unsigned long sz
= PAGE_SIZE
- realp
;
420 /* Hmm. Do something? */
432 * Handle first page in case it's not aligned
434 if (-realp
& (PAGE_SIZE
- 1))
435 sz
= -realp
& (PAGE_SIZE
- 1);
439 sz
= min_t(unsigned long, sz
, count
);
442 * On ia64 if a page has been mapped somewhere as
443 * uncached, then it must also be accessed uncached
444 * by the kernel or data corruption may occur
446 ptr
= xlate_dev_kmem_ptr(p
);
448 copied
= copy_from_user(ptr
, buf
, sz
);
450 written
+= sz
- copied
;
468 * This function writes to the *virtual* memory as seen by the kernel.
470 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
471 size_t count
, loff_t
*ppos
)
473 unsigned long p
= *ppos
;
477 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
479 if (p
< (unsigned long) high_memory
) {
482 if (count
> (unsigned long) high_memory
- p
)
483 wrote
= (unsigned long) high_memory
- p
;
485 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
486 if (written
!= wrote
)
495 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
497 return wrote
? wrote
: -ENOMEM
;
504 written
= copy_from_user(kbuf
, buf
, len
);
508 free_page((unsigned long)kbuf
);
512 len
= vwrite(kbuf
, (char *)p
, len
);
518 free_page((unsigned long)kbuf
);
522 return virtr
+ wrote
;
525 #if defined(CONFIG_ISA) || !defined(__mc68000__)
526 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
527 size_t count
, loff_t
*ppos
)
529 unsigned long i
= *ppos
;
530 char __user
*tmp
= buf
;
532 if (!access_ok(VERIFY_WRITE
, buf
, count
))
534 while (count
-- > 0 && i
< 65536) {
535 if (__put_user(inb(i
),tmp
) < 0)
544 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
545 size_t count
, loff_t
*ppos
)
547 unsigned long i
= *ppos
;
548 const char __user
* tmp
= buf
;
550 if (!access_ok(VERIFY_READ
,buf
,count
))
552 while (count
-- > 0 && i
< 65536) {
554 if (__get_user(c
, tmp
)) {
568 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
569 size_t count
, loff_t
*ppos
)
574 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
575 size_t count
, loff_t
*ppos
)
580 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
581 struct splice_desc
*sd
)
586 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
587 loff_t
*ppos
, size_t len
, unsigned int flags
)
589 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
594 * For fun, we are using the MMU for this.
596 static inline size_t read_zero_pagealigned(char __user
* buf
, size_t size
)
598 struct mm_struct
*mm
;
599 struct vm_area_struct
* vma
;
600 unsigned long addr
=(unsigned long)buf
;
603 /* Oops, this was forgotten before. -ben */
604 down_read(&mm
->mmap_sem
);
606 /* For private mappings, just map in zero pages. */
607 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
610 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
612 if (vma
->vm_flags
& (VM_SHARED
| VM_HUGETLB
))
614 count
= vma
->vm_end
- addr
;
618 zap_page_range(vma
, addr
, count
, NULL
);
619 if (zeromap_page_range(vma
, addr
, count
, PAGE_COPY
))
629 up_read(&mm
->mmap_sem
);
631 /* The shared case is hard. Let's do the conventional zeroing. */
633 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
635 return size
+ unwritten
- PAGE_SIZE
;
643 up_read(&mm
->mmap_sem
);
647 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
648 size_t count
, loff_t
*ppos
)
650 unsigned long left
, unwritten
, written
= 0;
655 if (!access_ok(VERIFY_WRITE
, buf
, count
))
660 /* do we want to be clever? Arbitrary cut-off */
661 if (count
>= PAGE_SIZE
*4) {
662 unsigned long partial
;
664 /* How much left of the page? */
665 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
666 unwritten
= clear_user(buf
, partial
);
667 written
= partial
- unwritten
;
672 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
673 written
+= (left
& PAGE_MASK
) - unwritten
;
676 buf
+= left
& PAGE_MASK
;
679 unwritten
= clear_user(buf
, left
);
680 written
+= left
- unwritten
;
682 return written
? written
: -EFAULT
;
685 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
689 if (vma
->vm_flags
& VM_SHARED
)
690 return shmem_zero_setup(vma
);
691 err
= zeromap_page_range(vma
, vma
->vm_start
,
692 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
693 BUG_ON(err
== -EEXIST
);
696 #else /* CONFIG_MMU */
697 static ssize_t
read_zero(struct file
* file
, char * buf
,
698 size_t count
, loff_t
*ppos
)
706 chunk
= 4096; /* Just for latency reasons */
707 if (clear_user(buf
, chunk
))
716 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
720 #endif /* CONFIG_MMU */
722 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
723 size_t count
, loff_t
*ppos
)
729 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
730 * can fopen() both devices with "a" now. This was previously impossible.
734 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
736 return file
->f_pos
= 0;
740 * The memory devices use the full 32/64 bits of the offset, and so we cannot
741 * check against negative addresses: they are ok. The return value is weird,
742 * though, in that case (0).
744 * also note that seeking relative to the "end of file" isn't supported:
745 * it has no meaning, so it returns -EINVAL.
747 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
751 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
754 file
->f_pos
= offset
;
756 force_successful_syscall_return();
759 file
->f_pos
+= offset
;
761 force_successful_syscall_return();
766 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
770 static int open_port(struct inode
* inode
, struct file
* filp
)
772 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
775 #define zero_lseek null_lseek
776 #define full_lseek null_lseek
777 #define write_zero write_null
778 #define read_full read_zero
779 #define open_mem open_port
780 #define open_kmem open_mem
781 #define open_oldmem open_mem
783 static const struct file_operations mem_fops
= {
784 .llseek
= memory_lseek
,
791 static const struct file_operations kmem_fops
= {
792 .llseek
= memory_lseek
,
799 static const struct file_operations null_fops
= {
800 .llseek
= null_lseek
,
803 .splice_write
= splice_write_null
,
806 #if defined(CONFIG_ISA) || !defined(__mc68000__)
807 static const struct file_operations port_fops
= {
808 .llseek
= memory_lseek
,
815 static const struct file_operations zero_fops
= {
816 .llseek
= zero_lseek
,
822 static struct backing_dev_info zero_bdi
= {
823 .capabilities
= BDI_CAP_MAP_COPY
,
826 static const struct file_operations full_fops
= {
827 .llseek
= full_lseek
,
832 #ifdef CONFIG_CRASH_DUMP
833 static const struct file_operations oldmem_fops
= {
839 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
840 size_t count
, loff_t
*ppos
)
845 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
849 if (!copy_from_user(tmp
, buf
, count
)) {
851 ret
= printk("%s", tmp
);
853 /* printk can add a prefix */
860 static const struct file_operations kmsg_fops
= {
864 static int memory_open(struct inode
* inode
, struct file
* filp
)
866 switch (iminor(inode
)) {
868 filp
->f_op
= &mem_fops
;
871 filp
->f_op
= &kmem_fops
;
874 filp
->f_op
= &null_fops
;
876 #if defined(CONFIG_ISA) || !defined(__mc68000__)
878 filp
->f_op
= &port_fops
;
882 filp
->f_mapping
->backing_dev_info
= &zero_bdi
;
883 filp
->f_op
= &zero_fops
;
886 filp
->f_op
= &full_fops
;
889 filp
->f_op
= &random_fops
;
892 filp
->f_op
= &urandom_fops
;
895 filp
->f_op
= &kmsg_fops
;
897 #ifdef CONFIG_CRASH_DUMP
899 filp
->f_op
= &oldmem_fops
;
905 if (filp
->f_op
&& filp
->f_op
->open
)
906 return filp
->f_op
->open(inode
,filp
);
910 static const struct file_operations memory_fops
= {
911 .open
= memory_open
, /* just a selector for the real open */
914 static const struct {
918 const struct file_operations
*fops
;
919 } devlist
[] = { /* list of minor devices */
920 {1, "mem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &mem_fops
},
921 {2, "kmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &kmem_fops
},
922 {3, "null", S_IRUGO
| S_IWUGO
, &null_fops
},
923 #if defined(CONFIG_ISA) || !defined(__mc68000__)
924 {4, "port", S_IRUSR
| S_IWUSR
| S_IRGRP
, &port_fops
},
926 {5, "zero", S_IRUGO
| S_IWUGO
, &zero_fops
},
927 {7, "full", S_IRUGO
| S_IWUGO
, &full_fops
},
928 {8, "random", S_IRUGO
| S_IWUSR
, &random_fops
},
929 {9, "urandom", S_IRUGO
| S_IWUSR
, &urandom_fops
},
930 {11,"kmsg", S_IRUGO
| S_IWUSR
, &kmsg_fops
},
931 #ifdef CONFIG_CRASH_DUMP
932 {12,"oldmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &oldmem_fops
},
936 static struct class *mem_class
;
938 static int __init
chr_dev_init(void)
942 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
943 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
945 mem_class
= class_create(THIS_MODULE
, "mem");
946 for (i
= 0; i
< ARRAY_SIZE(devlist
); i
++)
947 class_device_create(mem_class
, NULL
,
948 MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
949 NULL
, devlist
[i
].name
);
954 fs_initcall(chr_dev_init
);