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>
29 #include <linux/smp_lock.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
)
45 #if defined(CONFIG_IA64)
47 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
49 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
50 #elif defined(CONFIG_MIPS)
52 extern int __uncached_access(struct file
*file
,
55 return __uncached_access(file
, addr
);
59 * Accessing memory above the top the kernel knows about or through a file pointer
60 * that was marked O_SYNC will be done non-cached.
62 if (file
->f_flags
& O_SYNC
)
64 return addr
>= __pa(high_memory
);
68 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
69 static inline int valid_phys_addr_range(unsigned long addr
, size_t count
)
71 if (addr
+ count
> __pa(high_memory
))
77 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
83 #ifdef CONFIG_STRICT_DEVMEM
84 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
86 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
91 if (!devmem_is_allowed(pfn
)) {
93 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
94 current
->comm
, from
, to
);
103 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
109 void __attribute__((weak
)) unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
114 * This funcion reads the *physical* memory. The f_pos points directly to the
117 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
118 size_t count
, loff_t
*ppos
)
120 unsigned long p
= *ppos
;
124 if (!valid_phys_addr_range(p
, count
))
127 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
128 /* we don't have page 0 mapped on sparc and m68k.. */
134 if (clear_user(buf
, sz
))
146 * Handle first page in case it's not aligned
148 if (-p
& (PAGE_SIZE
- 1))
149 sz
= -p
& (PAGE_SIZE
- 1);
153 sz
= min_t(unsigned long, sz
, count
);
155 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
))
159 * On ia64 if a page has been mapped somewhere as
160 * uncached, then it must also be accessed uncached
161 * by the kernel or data corruption may occur
163 ptr
= xlate_dev_mem_ptr(p
);
167 if (copy_to_user(buf
, ptr
, sz
)) {
168 unxlate_dev_mem_ptr(p
, ptr
);
172 unxlate_dev_mem_ptr(p
, ptr
);
184 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
185 size_t count
, loff_t
*ppos
)
187 unsigned long p
= *ppos
;
189 unsigned long copied
;
192 if (!valid_phys_addr_range(p
, count
))
197 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
198 /* we don't have page 0 mapped on sparc and m68k.. */
200 unsigned long sz
= PAGE_SIZE
- p
;
203 /* Hmm. Do something? */
213 * Handle first page in case it's not aligned
215 if (-p
& (PAGE_SIZE
- 1))
216 sz
= -p
& (PAGE_SIZE
- 1);
220 sz
= min_t(unsigned long, sz
, count
);
222 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
226 * On ia64 if a page has been mapped somewhere as
227 * uncached, then it must also be accessed uncached
228 * by the kernel or data corruption may occur
230 ptr
= xlate_dev_mem_ptr(p
);
237 copied
= copy_from_user(ptr
, buf
, sz
);
239 written
+= sz
- copied
;
240 unxlate_dev_mem_ptr(p
, ptr
);
246 unxlate_dev_mem_ptr(p
, ptr
);
258 int __attribute__((weak
)) phys_mem_access_prot_allowed(struct file
*file
,
259 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
264 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
265 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
266 unsigned long size
, pgprot_t vma_prot
)
268 #ifdef pgprot_noncached
269 unsigned long offset
= pfn
<< PAGE_SHIFT
;
271 if (uncached_access(file
, offset
))
272 return pgprot_noncached(vma_prot
);
279 static unsigned long get_unmapped_area_mem(struct file
*file
,
285 if (!valid_mmap_phys_addr_range(pgoff
, len
))
286 return (unsigned long) -EINVAL
;
287 return pgoff
<< PAGE_SHIFT
;
290 /* can't do an in-place private mapping if there's no MMU */
291 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
293 return vma
->vm_flags
& VM_MAYSHARE
;
296 #define get_unmapped_area_mem NULL
298 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
304 static const struct vm_operations_struct mmap_mem_ops
= {
305 #ifdef CONFIG_HAVE_IOREMAP_PROT
306 .access
= generic_access_phys
310 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
312 size_t size
= vma
->vm_end
- vma
->vm_start
;
314 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
317 if (!private_mapping_ok(vma
))
320 if (!range_is_allowed(vma
->vm_pgoff
, size
))
323 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
327 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
331 vma
->vm_ops
= &mmap_mem_ops
;
333 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
334 if (remap_pfn_range(vma
,
338 vma
->vm_page_prot
)) {
344 #ifdef CONFIG_DEVKMEM
345 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
349 /* Turn a kernel-virtual address into a physical page frame */
350 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
353 * RED-PEN: on some architectures there is more mapped memory
354 * than available in mem_map which pfn_valid checks
355 * for. Perhaps should add a new macro here.
357 * RED-PEN: vmalloc is not supported right now.
363 return mmap_mem(file
, vma
);
367 #ifdef CONFIG_CRASH_DUMP
369 * Read memory corresponding to the old kernel.
371 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
372 size_t count
, loff_t
*ppos
)
374 unsigned long pfn
, offset
;
375 size_t read
= 0, csize
;
379 pfn
= *ppos
/ PAGE_SIZE
;
380 if (pfn
> saved_max_pfn
)
383 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
384 if (count
> PAGE_SIZE
- offset
)
385 csize
= PAGE_SIZE
- offset
;
389 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
401 #ifdef CONFIG_DEVKMEM
403 * This function reads the *virtual* memory as seen by the kernel.
405 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
406 size_t count
, loff_t
*ppos
)
408 unsigned long p
= *ppos
;
409 ssize_t low_count
, read
, sz
;
410 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
413 if (p
< (unsigned long) high_memory
) {
415 if (count
> (unsigned long) high_memory
- p
)
416 low_count
= (unsigned long) high_memory
- p
;
418 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
419 /* we don't have page 0 mapped on sparc and m68k.. */
420 if (p
< PAGE_SIZE
&& low_count
> 0) {
421 size_t tmp
= PAGE_SIZE
- p
;
422 if (tmp
> low_count
) tmp
= low_count
;
423 if (clear_user(buf
, tmp
))
432 while (low_count
> 0) {
434 * Handle first page in case it's not aligned
436 if (-p
& (PAGE_SIZE
- 1))
437 sz
= -p
& (PAGE_SIZE
- 1);
441 sz
= min_t(unsigned long, sz
, low_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 kbuf
= xlate_dev_kmem_ptr((char *)p
);
450 if (copy_to_user(buf
, kbuf
, sz
))
461 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
469 len
= vread(kbuf
, (char *)p
, len
);
472 if (copy_to_user(buf
, kbuf
, len
)) {
473 free_page((unsigned long)kbuf
);
481 free_page((unsigned long)kbuf
);
488 static inline ssize_t
489 do_write_kmem(void *p
, unsigned long realp
, const char __user
* buf
,
490 size_t count
, loff_t
*ppos
)
493 unsigned long copied
;
496 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
497 /* we don't have page 0 mapped on sparc and m68k.. */
498 if (realp
< PAGE_SIZE
) {
499 unsigned long sz
= PAGE_SIZE
- realp
;
502 /* Hmm. Do something? */
514 * Handle first page in case it's not aligned
516 if (-realp
& (PAGE_SIZE
- 1))
517 sz
= -realp
& (PAGE_SIZE
- 1);
521 sz
= min_t(unsigned long, sz
, count
);
524 * On ia64 if a page has been mapped somewhere as
525 * uncached, then it must also be accessed uncached
526 * by the kernel or data corruption may occur
528 ptr
= xlate_dev_kmem_ptr(p
);
530 copied
= copy_from_user(ptr
, buf
, sz
);
532 written
+= sz
- copied
;
550 * This function writes to the *virtual* memory as seen by the kernel.
552 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
553 size_t count
, loff_t
*ppos
)
555 unsigned long p
= *ppos
;
559 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
561 if (p
< (unsigned long) high_memory
) {
564 if (count
> (unsigned long) high_memory
- p
)
565 wrote
= (unsigned long) high_memory
- p
;
567 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
568 if (written
!= wrote
)
577 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
579 return wrote
? wrote
: -ENOMEM
;
586 written
= copy_from_user(kbuf
, buf
, len
);
590 free_page((unsigned long)kbuf
);
594 len
= vwrite(kbuf
, (char *)p
, len
);
600 free_page((unsigned long)kbuf
);
604 return virtr
+ wrote
;
608 #ifdef CONFIG_DEVPORT
609 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
610 size_t count
, loff_t
*ppos
)
612 unsigned long i
= *ppos
;
613 char __user
*tmp
= buf
;
615 if (!access_ok(VERIFY_WRITE
, buf
, count
))
617 while (count
-- > 0 && i
< 65536) {
618 if (__put_user(inb(i
),tmp
) < 0)
627 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
628 size_t count
, loff_t
*ppos
)
630 unsigned long i
= *ppos
;
631 const char __user
* tmp
= buf
;
633 if (!access_ok(VERIFY_READ
,buf
,count
))
635 while (count
-- > 0 && i
< 65536) {
637 if (__get_user(c
, tmp
)) {
651 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
652 size_t count
, loff_t
*ppos
)
657 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
658 size_t count
, loff_t
*ppos
)
663 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
664 struct splice_desc
*sd
)
669 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
670 loff_t
*ppos
, size_t len
, unsigned int flags
)
672 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
675 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
676 size_t count
, loff_t
*ppos
)
683 if (!access_ok(VERIFY_WRITE
, buf
, count
))
688 unsigned long unwritten
;
689 size_t chunk
= count
;
691 if (chunk
> PAGE_SIZE
)
692 chunk
= PAGE_SIZE
; /* Just for latency reasons */
693 unwritten
= __clear_user(buf
, chunk
);
694 written
+= chunk
- unwritten
;
697 if (signal_pending(current
))
698 return written
? written
: -ERESTARTSYS
;
703 return written
? written
: -EFAULT
;
706 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
711 if (vma
->vm_flags
& VM_SHARED
)
712 return shmem_zero_setup(vma
);
716 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
717 size_t count
, loff_t
*ppos
)
723 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
724 * can fopen() both devices with "a" now. This was previously impossible.
728 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
730 return file
->f_pos
= 0;
734 * The memory devices use the full 32/64 bits of the offset, and so we cannot
735 * check against negative addresses: they are ok. The return value is weird,
736 * though, in that case (0).
738 * also note that seeking relative to the "end of file" isn't supported:
739 * it has no meaning, so it returns -EINVAL.
741 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
745 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
748 file
->f_pos
= offset
;
750 force_successful_syscall_return();
753 file
->f_pos
+= offset
;
755 force_successful_syscall_return();
760 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
764 static int open_port(struct inode
* inode
, struct file
* filp
)
766 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
769 #define zero_lseek null_lseek
770 #define full_lseek null_lseek
771 #define write_zero write_null
772 #define read_full read_zero
773 #define open_mem open_port
774 #define open_kmem open_mem
775 #define open_oldmem open_mem
777 static const struct file_operations mem_fops
= {
778 .llseek
= memory_lseek
,
783 .get_unmapped_area
= get_unmapped_area_mem
,
786 #ifdef CONFIG_DEVKMEM
787 static const struct file_operations kmem_fops
= {
788 .llseek
= memory_lseek
,
793 .get_unmapped_area
= get_unmapped_area_mem
,
797 static const struct file_operations null_fops
= {
798 .llseek
= null_lseek
,
801 .splice_write
= splice_write_null
,
804 #ifdef CONFIG_DEVPORT
805 static const struct file_operations port_fops
= {
806 .llseek
= memory_lseek
,
813 static const struct file_operations zero_fops
= {
814 .llseek
= zero_lseek
,
821 * capabilities for /dev/zero
822 * - permits private mappings, "copies" are taken of the source of zeros
824 static struct backing_dev_info zero_bdi
= {
826 .capabilities
= BDI_CAP_MAP_COPY
,
829 static const struct file_operations full_fops
= {
830 .llseek
= full_lseek
,
835 #ifdef CONFIG_CRASH_DUMP
836 static const struct file_operations oldmem_fops
= {
842 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
843 size_t count
, loff_t
*ppos
)
848 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
852 if (!copy_from_user(tmp
, buf
, count
)) {
854 ret
= printk("%s", tmp
);
856 /* printk can add a prefix */
863 static const struct file_operations kmsg_fops
= {
867 static const struct memdev
{
870 const struct file_operations
*fops
;
871 struct backing_dev_info
*dev_info
;
873 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
874 #ifdef CONFIG_DEVKMEM
875 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
877 [3] = { "null", 0666, &null_fops
, NULL
},
878 #ifdef CONFIG_DEVPORT
879 [4] = { "port", 0, &port_fops
, NULL
},
881 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
882 [7] = { "full", 0666, &full_fops
, NULL
},
883 [8] = { "random", 0666, &random_fops
, NULL
},
884 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
885 [11] = { "kmsg", 0, &kmsg_fops
, NULL
},
886 #ifdef CONFIG_CRASH_DUMP
887 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
891 static int memory_open(struct inode
*inode
, struct file
*filp
)
894 const struct memdev
*dev
;
899 minor
= iminor(inode
);
900 if (minor
>= ARRAY_SIZE(devlist
))
903 dev
= &devlist
[minor
];
907 filp
->f_op
= dev
->fops
;
909 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
912 ret
= dev
->fops
->open(inode
, filp
);
920 static const struct file_operations memory_fops
= {
924 static char *mem_devnode(struct device
*dev
, mode_t
*mode
)
926 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
927 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
931 static struct class *mem_class
;
933 static int __init
chr_dev_init(void)
938 err
= bdi_init(&zero_bdi
);
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 mem_class
->devnode
= mem_devnode
;
947 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
948 if (!devlist
[minor
].name
)
950 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
951 NULL
, devlist
[minor
].name
);
957 fs_initcall(chr_dev_init
);