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 mmapping 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(CONFIG_IA64)
46 * On ia64, we ignore O_DSYNC because we cannot tolerate memory attribute aliases.
48 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
49 #elif defined(CONFIG_MIPS)
51 extern int __uncached_access(struct file
*file
,
54 return __uncached_access(file
, addr
);
58 * Accessing memory above the top the kernel knows about or through a file pointer
59 * that was marked O_DSYNC will be done non-cached.
61 if (file
->f_flags
& O_DSYNC
)
63 return addr
>= __pa(high_memory
);
67 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
68 static inline int valid_phys_addr_range(unsigned long addr
, size_t count
)
70 if (addr
+ count
> __pa(high_memory
))
76 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
82 #ifdef CONFIG_STRICT_DEVMEM
83 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
85 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
90 if (!devmem_is_allowed(pfn
)) {
92 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
93 current
->comm
, from
, to
);
102 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
108 void __attribute__((weak
)) unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
113 * This funcion reads the *physical* memory. The f_pos points directly to the
116 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
117 size_t count
, loff_t
*ppos
)
119 unsigned long p
= *ppos
;
123 if (!valid_phys_addr_range(p
, count
))
126 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
127 /* we don't have page 0 mapped on sparc and m68k.. */
133 if (clear_user(buf
, sz
))
145 * Handle first page in case it's not aligned
147 if (-p
& (PAGE_SIZE
- 1))
148 sz
= -p
& (PAGE_SIZE
- 1);
152 sz
= min_t(unsigned long, sz
, count
);
154 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
))
158 * On ia64 if a page has been mapped somewhere as
159 * uncached, then it must also be accessed uncached
160 * by the kernel or data corruption may occur
162 ptr
= xlate_dev_mem_ptr(p
);
166 if (copy_to_user(buf
, ptr
, sz
)) {
167 unxlate_dev_mem_ptr(p
, ptr
);
171 unxlate_dev_mem_ptr(p
, ptr
);
183 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
184 size_t count
, loff_t
*ppos
)
186 unsigned long p
= *ppos
;
188 unsigned long copied
;
191 if (!valid_phys_addr_range(p
, count
))
196 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
197 /* we don't have page 0 mapped on sparc and m68k.. */
199 unsigned long sz
= PAGE_SIZE
- p
;
202 /* Hmm. Do something? */
212 * Handle first page in case it's not aligned
214 if (-p
& (PAGE_SIZE
- 1))
215 sz
= -p
& (PAGE_SIZE
- 1);
219 sz
= min_t(unsigned long, sz
, count
);
221 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
225 * On ia64 if a page has been mapped somewhere as
226 * uncached, then it must also be accessed uncached
227 * by the kernel or data corruption may occur
229 ptr
= xlate_dev_mem_ptr(p
);
236 copied
= copy_from_user(ptr
, buf
, sz
);
238 written
+= sz
- copied
;
239 unxlate_dev_mem_ptr(p
, ptr
);
245 unxlate_dev_mem_ptr(p
, ptr
);
257 int __attribute__((weak
)) phys_mem_access_prot_allowed(struct file
*file
,
258 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
263 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
264 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
265 unsigned long size
, pgprot_t vma_prot
)
267 #ifdef pgprot_noncached
268 unsigned long offset
= pfn
<< PAGE_SHIFT
;
270 if (uncached_access(file
, offset
))
271 return pgprot_noncached(vma_prot
);
278 static unsigned long get_unmapped_area_mem(struct file
*file
,
284 if (!valid_mmap_phys_addr_range(pgoff
, len
))
285 return (unsigned long) -EINVAL
;
286 return pgoff
<< PAGE_SHIFT
;
289 /* can't do an in-place private mapping if there's no MMU */
290 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
292 return vma
->vm_flags
& VM_MAYSHARE
;
295 #define get_unmapped_area_mem NULL
297 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
303 static const struct vm_operations_struct mmap_mem_ops
= {
304 #ifdef CONFIG_HAVE_IOREMAP_PROT
305 .access
= generic_access_phys
309 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
311 size_t size
= vma
->vm_end
- vma
->vm_start
;
313 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
316 if (!private_mapping_ok(vma
))
319 if (!range_is_allowed(vma
->vm_pgoff
, size
))
322 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
326 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
330 vma
->vm_ops
= &mmap_mem_ops
;
332 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
333 if (remap_pfn_range(vma
,
337 vma
->vm_page_prot
)) {
343 #ifdef CONFIG_DEVKMEM
344 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
348 /* Turn a kernel-virtual address into a physical page frame */
349 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
352 * RED-PEN: on some architectures there is more mapped memory
353 * than available in mem_map which pfn_valid checks
354 * for. Perhaps should add a new macro here.
356 * RED-PEN: vmalloc is not supported right now.
362 return mmap_mem(file
, vma
);
366 #ifdef CONFIG_CRASH_DUMP
368 * Read memory corresponding to the old kernel.
370 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
371 size_t count
, loff_t
*ppos
)
373 unsigned long pfn
, offset
;
374 size_t read
= 0, csize
;
378 pfn
= *ppos
/ PAGE_SIZE
;
379 if (pfn
> saved_max_pfn
)
382 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
383 if (count
> PAGE_SIZE
- offset
)
384 csize
= PAGE_SIZE
- offset
;
388 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
400 #ifdef CONFIG_DEVKMEM
402 * This function reads the *virtual* memory as seen by the kernel.
404 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
405 size_t count
, loff_t
*ppos
)
407 unsigned long p
= *ppos
;
408 ssize_t low_count
, read
, sz
;
409 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
412 if (p
< (unsigned long) high_memory
) {
414 if (count
> (unsigned long) high_memory
- p
)
415 low_count
= (unsigned long) high_memory
- p
;
417 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
418 /* we don't have page 0 mapped on sparc and m68k.. */
419 if (p
< PAGE_SIZE
&& low_count
> 0) {
420 size_t tmp
= PAGE_SIZE
- p
;
421 if (tmp
> low_count
) tmp
= low_count
;
422 if (clear_user(buf
, tmp
))
431 while (low_count
> 0) {
433 * Handle first page in case it's not aligned
435 if (-p
& (PAGE_SIZE
- 1))
436 sz
= -p
& (PAGE_SIZE
- 1);
440 sz
= min_t(unsigned long, sz
, low_count
);
443 * On ia64 if a page has been mapped somewhere as
444 * uncached, then it must also be accessed uncached
445 * by the kernel or data corruption may occur
447 kbuf
= xlate_dev_kmem_ptr((char *)p
);
449 if (copy_to_user(buf
, kbuf
, sz
))
460 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
468 len
= vread(kbuf
, (char *)p
, len
);
471 if (copy_to_user(buf
, kbuf
, len
)) {
472 free_page((unsigned long)kbuf
);
480 free_page((unsigned long)kbuf
);
487 static inline ssize_t
488 do_write_kmem(void *p
, unsigned long realp
, const char __user
* buf
,
489 size_t count
, loff_t
*ppos
)
492 unsigned long copied
;
495 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
496 /* we don't have page 0 mapped on sparc and m68k.. */
497 if (realp
< PAGE_SIZE
) {
498 unsigned long sz
= PAGE_SIZE
- realp
;
501 /* Hmm. Do something? */
513 * Handle first page in case it's not aligned
515 if (-realp
& (PAGE_SIZE
- 1))
516 sz
= -realp
& (PAGE_SIZE
- 1);
520 sz
= min_t(unsigned long, sz
, count
);
523 * On ia64 if a page has been mapped somewhere as
524 * uncached, then it must also be accessed uncached
525 * by the kernel or data corruption may occur
527 ptr
= xlate_dev_kmem_ptr(p
);
529 copied
= copy_from_user(ptr
, buf
, sz
);
531 written
+= sz
- copied
;
549 * This function writes to the *virtual* memory as seen by the kernel.
551 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
552 size_t count
, loff_t
*ppos
)
554 unsigned long p
= *ppos
;
558 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
560 if (p
< (unsigned long) high_memory
) {
563 if (count
> (unsigned long) high_memory
- p
)
564 wrote
= (unsigned long) high_memory
- p
;
566 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
567 if (written
!= wrote
)
576 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
578 return wrote
? wrote
: -ENOMEM
;
585 written
= copy_from_user(kbuf
, buf
, len
);
589 free_page((unsigned long)kbuf
);
593 len
= vwrite(kbuf
, (char *)p
, len
);
599 free_page((unsigned long)kbuf
);
603 return virtr
+ wrote
;
607 #ifdef CONFIG_DEVPORT
608 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
609 size_t count
, loff_t
*ppos
)
611 unsigned long i
= *ppos
;
612 char __user
*tmp
= buf
;
614 if (!access_ok(VERIFY_WRITE
, buf
, count
))
616 while (count
-- > 0 && i
< 65536) {
617 if (__put_user(inb(i
),tmp
) < 0)
626 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
627 size_t count
, loff_t
*ppos
)
629 unsigned long i
= *ppos
;
630 const char __user
* tmp
= buf
;
632 if (!access_ok(VERIFY_READ
,buf
,count
))
634 while (count
-- > 0 && i
< 65536) {
636 if (__get_user(c
, tmp
)) {
650 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
651 size_t count
, loff_t
*ppos
)
656 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
657 size_t count
, loff_t
*ppos
)
662 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
663 struct splice_desc
*sd
)
668 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
669 loff_t
*ppos
, size_t len
, unsigned int flags
)
671 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
674 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
675 size_t count
, loff_t
*ppos
)
682 if (!access_ok(VERIFY_WRITE
, buf
, count
))
687 unsigned long unwritten
;
688 size_t chunk
= count
;
690 if (chunk
> PAGE_SIZE
)
691 chunk
= PAGE_SIZE
; /* Just for latency reasons */
692 unwritten
= __clear_user(buf
, chunk
);
693 written
+= chunk
- unwritten
;
696 if (signal_pending(current
))
697 return written
? written
: -ERESTARTSYS
;
702 return written
? written
: -EFAULT
;
705 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
710 if (vma
->vm_flags
& VM_SHARED
)
711 return shmem_zero_setup(vma
);
715 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
716 size_t count
, loff_t
*ppos
)
722 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
723 * can fopen() both devices with "a" now. This was previously impossible.
727 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
729 return file
->f_pos
= 0;
733 * The memory devices use the full 32/64 bits of the offset, and so we cannot
734 * check against negative addresses: they are ok. The return value is weird,
735 * though, in that case (0).
737 * also note that seeking relative to the "end of file" isn't supported:
738 * it has no meaning, so it returns -EINVAL.
740 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
744 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
747 file
->f_pos
= offset
;
749 force_successful_syscall_return();
752 file
->f_pos
+= offset
;
754 force_successful_syscall_return();
759 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
763 static int open_port(struct inode
* inode
, struct file
* filp
)
765 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
768 #define zero_lseek null_lseek
769 #define full_lseek null_lseek
770 #define write_zero write_null
771 #define read_full read_zero
772 #define open_mem open_port
773 #define open_kmem open_mem
774 #define open_oldmem open_mem
776 static const struct file_operations mem_fops
= {
777 .llseek
= memory_lseek
,
782 .get_unmapped_area
= get_unmapped_area_mem
,
785 #ifdef CONFIG_DEVKMEM
786 static const struct file_operations kmem_fops
= {
787 .llseek
= memory_lseek
,
792 .get_unmapped_area
= get_unmapped_area_mem
,
796 static const struct file_operations null_fops
= {
797 .llseek
= null_lseek
,
800 .splice_write
= splice_write_null
,
803 #ifdef CONFIG_DEVPORT
804 static const struct file_operations port_fops
= {
805 .llseek
= memory_lseek
,
812 static const struct file_operations zero_fops
= {
813 .llseek
= zero_lseek
,
820 * capabilities for /dev/zero
821 * - permits private mappings, "copies" are taken of the source of zeros
823 static struct backing_dev_info zero_bdi
= {
825 .capabilities
= BDI_CAP_MAP_COPY
,
828 static const struct file_operations full_fops
= {
829 .llseek
= full_lseek
,
834 #ifdef CONFIG_CRASH_DUMP
835 static const struct file_operations oldmem_fops
= {
841 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
842 size_t count
, loff_t
*ppos
)
847 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
851 if (!copy_from_user(tmp
, buf
, count
)) {
853 ret
= printk("%s", tmp
);
855 /* printk can add a prefix */
862 static const struct file_operations kmsg_fops
= {
866 static const struct memdev
{
869 const struct file_operations
*fops
;
870 struct backing_dev_info
*dev_info
;
872 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
873 #ifdef CONFIG_DEVKMEM
874 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
876 [3] = { "null", 0666, &null_fops
, NULL
},
877 #ifdef CONFIG_DEVPORT
878 [4] = { "port", 0, &port_fops
, NULL
},
880 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
881 [7] = { "full", 0666, &full_fops
, NULL
},
882 [8] = { "random", 0666, &random_fops
, NULL
},
883 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
884 [11] = { "kmsg", 0, &kmsg_fops
, NULL
},
885 #ifdef CONFIG_CRASH_DUMP
886 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
890 static int memory_open(struct inode
*inode
, struct file
*filp
)
893 const struct memdev
*dev
;
895 minor
= iminor(inode
);
896 if (minor
>= ARRAY_SIZE(devlist
))
899 dev
= &devlist
[minor
];
903 filp
->f_op
= dev
->fops
;
905 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
908 return dev
->fops
->open(inode
, filp
);
913 static const struct file_operations memory_fops
= {
917 static char *mem_devnode(struct device
*dev
, mode_t
*mode
)
919 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
920 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
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 mem_class
->devnode
= mem_devnode
;
940 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
941 if (!devlist
[minor
].name
)
943 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
944 NULL
, devlist
[minor
].name
);
950 fs_initcall(chr_dev_init
);