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>
29 #include <linux/export.h>
31 #include <linux/aio.h>
33 #include <asm/uaccess.h>
36 # include <linux/efi.h>
39 #define DEVPORT_MINOR 4
41 static inline unsigned long size_inside_page(unsigned long start
,
46 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
51 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
52 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
54 return addr
+ count
<= __pa(high_memory
);
57 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
63 #ifdef CONFIG_STRICT_DEVMEM
64 static inline int page_is_allowed(unsigned long pfn
)
66 return devmem_is_allowed(pfn
);
68 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
70 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
75 if (!devmem_is_allowed(pfn
))
83 static inline int page_is_allowed(unsigned long pfn
)
87 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
93 void __weak
unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
98 * This funcion reads the *physical* memory. The f_pos points directly to the
101 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
102 size_t count
, loff_t
*ppos
)
104 phys_addr_t p
= *ppos
;
108 if (!valid_phys_addr_range(p
, count
))
111 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
112 /* we don't have page 0 mapped on sparc and m68k.. */
114 sz
= size_inside_page(p
, count
);
116 if (clear_user(buf
, sz
))
127 unsigned long remaining
;
130 sz
= size_inside_page(p
, count
);
132 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
136 /* Show zeros for restricted memory. */
137 remaining
= clear_user(buf
, sz
);
140 * On ia64 if a page has been mapped somewhere as
141 * uncached, then it must also be accessed uncached
142 * by the kernel or data corruption may occur.
144 ptr
= xlate_dev_mem_ptr(p
);
148 remaining
= copy_to_user(buf
, ptr
, sz
);
150 unxlate_dev_mem_ptr(p
, ptr
);
166 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
167 size_t count
, loff_t
*ppos
)
169 phys_addr_t 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 sz
= size_inside_page(p
, count
);
183 /* Hmm. Do something? */
194 sz
= size_inside_page(p
, count
);
196 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
200 /* Skip actual writing when a page is marked as restricted. */
203 * On ia64 if a page has been mapped somewhere as
204 * uncached, then it must also be accessed uncached
205 * by the kernel or data corruption may occur.
207 ptr
= xlate_dev_mem_ptr(p
);
214 copied
= copy_from_user(ptr
, buf
, sz
);
215 unxlate_dev_mem_ptr(p
, ptr
);
217 written
+= sz
- copied
;
234 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
235 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
240 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
243 * Architectures vary in how they handle caching for addresses
244 * outside of main memory.
247 #ifdef pgprot_noncached
248 static int uncached_access(struct file
*file
, phys_addr_t addr
)
250 #if defined(CONFIG_IA64)
252 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
255 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
256 #elif defined(CONFIG_MIPS)
258 extern int __uncached_access(struct file
*file
,
261 return __uncached_access(file
, addr
);
265 * Accessing memory above the top the kernel knows about or through a
267 * that was marked O_DSYNC will be done non-cached.
269 if (file
->f_flags
& O_DSYNC
)
271 return addr
>= __pa(high_memory
);
276 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
277 unsigned long size
, pgprot_t vma_prot
)
279 #ifdef pgprot_noncached
280 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
282 if (uncached_access(file
, offset
))
283 return pgprot_noncached(vma_prot
);
290 static unsigned long get_unmapped_area_mem(struct file
*file
,
296 if (!valid_mmap_phys_addr_range(pgoff
, len
))
297 return (unsigned long) -EINVAL
;
298 return pgoff
<< PAGE_SHIFT
;
301 /* can't do an in-place private mapping if there's no MMU */
302 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
304 return vma
->vm_flags
& VM_MAYSHARE
;
307 #define get_unmapped_area_mem NULL
309 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
315 static const struct vm_operations_struct mmap_mem_ops
= {
316 #ifdef CONFIG_HAVE_IOREMAP_PROT
317 .access
= generic_access_phys
321 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
323 size_t size
= vma
->vm_end
- vma
->vm_start
;
325 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
328 if (!private_mapping_ok(vma
))
331 if (!range_is_allowed(vma
->vm_pgoff
, size
))
334 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
338 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
342 vma
->vm_ops
= &mmap_mem_ops
;
344 /* Remap-pfn-range will mark the range VM_IO */
345 if (remap_pfn_range(vma
,
349 vma
->vm_page_prot
)) {
355 #ifdef CONFIG_DEVKMEM
356 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
360 /* Turn a kernel-virtual address into a physical page frame */
361 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
364 * RED-PEN: on some architectures there is more mapped memory than
365 * available in mem_map which pfn_valid checks for. Perhaps should add a
368 * RED-PEN: vmalloc is not supported right now.
374 return mmap_mem(file
, vma
);
378 #ifdef CONFIG_CRASH_DUMP
380 * Read memory corresponding to the old kernel.
382 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
383 size_t count
, loff_t
*ppos
)
385 unsigned long pfn
, offset
;
386 size_t read
= 0, csize
;
390 pfn
= *ppos
/ PAGE_SIZE
;
391 if (pfn
> saved_max_pfn
)
394 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
395 if (count
> PAGE_SIZE
- offset
)
396 csize
= PAGE_SIZE
- offset
;
400 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
412 #ifdef CONFIG_DEVKMEM
414 * This function reads the *virtual* memory as seen by the kernel.
416 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
417 size_t count
, loff_t
*ppos
)
419 unsigned long p
= *ppos
;
420 ssize_t low_count
, read
, sz
;
421 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
425 if (p
< (unsigned long) high_memory
) {
427 if (count
> (unsigned long)high_memory
- p
)
428 low_count
= (unsigned long)high_memory
- p
;
430 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
431 /* we don't have page 0 mapped on sparc and m68k.. */
432 if (p
< PAGE_SIZE
&& low_count
> 0) {
433 sz
= size_inside_page(p
, low_count
);
434 if (clear_user(buf
, sz
))
443 while (low_count
> 0) {
444 sz
= size_inside_page(p
, low_count
);
447 * On ia64 if a page has been mapped somewhere as
448 * uncached, then it must also be accessed uncached
449 * by the kernel or data corruption may occur
451 kbuf
= xlate_dev_kmem_ptr((char *)p
);
453 if (copy_to_user(buf
, kbuf
, sz
))
464 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
468 sz
= size_inside_page(p
, count
);
469 if (!is_vmalloc_or_module_addr((void *)p
)) {
473 sz
= vread(kbuf
, (char *)p
, sz
);
476 if (copy_to_user(buf
, kbuf
, sz
)) {
485 free_page((unsigned long)kbuf
);
488 return read
? read
: err
;
492 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
493 size_t count
, loff_t
*ppos
)
496 unsigned long copied
;
499 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
500 /* we don't have page 0 mapped on sparc and m68k.. */
502 sz
= size_inside_page(p
, count
);
503 /* Hmm. Do something? */
514 sz
= size_inside_page(p
, count
);
517 * On ia64 if a page has been mapped somewhere as uncached, then
518 * it must also be accessed uncached by the kernel or data
519 * corruption may occur.
521 ptr
= xlate_dev_kmem_ptr((char *)p
);
523 copied
= copy_from_user(ptr
, buf
, sz
);
525 written
+= sz
- copied
;
541 * This function writes to the *virtual* memory as seen by the kernel.
543 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
544 size_t count
, loff_t
*ppos
)
546 unsigned long p
= *ppos
;
549 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
552 if (p
< (unsigned long) high_memory
) {
553 unsigned long to_write
= min_t(unsigned long, count
,
554 (unsigned long)high_memory
- p
);
555 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
556 if (wrote
!= to_write
)
564 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
566 return wrote
? wrote
: -ENOMEM
;
568 unsigned long sz
= size_inside_page(p
, count
);
571 if (!is_vmalloc_or_module_addr((void *)p
)) {
575 n
= copy_from_user(kbuf
, buf
, sz
);
580 vwrite(kbuf
, (char *)p
, sz
);
586 free_page((unsigned long)kbuf
);
590 return virtr
+ wrote
? : err
;
594 #ifdef CONFIG_DEVPORT
595 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
596 size_t count
, loff_t
*ppos
)
598 unsigned long i
= *ppos
;
599 char __user
*tmp
= buf
;
601 if (!access_ok(VERIFY_WRITE
, buf
, count
))
603 while (count
-- > 0 && i
< 65536) {
604 if (__put_user(inb(i
), tmp
) < 0)
613 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
614 size_t count
, loff_t
*ppos
)
616 unsigned long i
= *ppos
;
617 const char __user
*tmp
= buf
;
619 if (!access_ok(VERIFY_READ
, buf
, count
))
621 while (count
-- > 0 && i
< 65536) {
623 if (__get_user(c
, tmp
)) {
637 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
638 size_t count
, loff_t
*ppos
)
643 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
644 size_t count
, loff_t
*ppos
)
649 static ssize_t
aio_read_null(struct kiocb
*iocb
, const struct iovec
*iov
,
650 unsigned long nr_segs
, loff_t pos
)
655 static ssize_t
aio_write_null(struct kiocb
*iocb
, const struct iovec
*iov
,
656 unsigned long nr_segs
, loff_t pos
)
658 return iov_length(iov
, nr_segs
);
661 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
662 struct splice_desc
*sd
)
667 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
668 loff_t
*ppos
, size_t len
, unsigned int flags
)
670 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
673 static ssize_t
read_zero(struct file
*file
, char __user
*buf
,
674 size_t count
, loff_t
*ppos
)
681 if (!access_ok(VERIFY_WRITE
, buf
, count
))
686 unsigned long unwritten
;
687 size_t chunk
= count
;
689 if (chunk
> PAGE_SIZE
)
690 chunk
= PAGE_SIZE
; /* Just for latency reasons */
691 unwritten
= __clear_user(buf
, chunk
);
692 written
+= chunk
- unwritten
;
695 if (signal_pending(current
))
696 return written
? written
: -ERESTARTSYS
;
701 return written
? written
: -EFAULT
;
704 static ssize_t
aio_read_zero(struct kiocb
*iocb
, const struct iovec
*iov
,
705 unsigned long nr_segs
, loff_t pos
)
711 for (i
= 0; i
< nr_segs
; i
++) {
712 ret
= read_zero(iocb
->ki_filp
, iov
[i
].iov_base
, iov
[i
].iov_len
,
719 return written
? written
: -EFAULT
;
722 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
727 if (vma
->vm_flags
& VM_SHARED
)
728 return shmem_zero_setup(vma
);
732 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
733 size_t count
, loff_t
*ppos
)
739 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
740 * can fopen() both devices with "a" now. This was previously impossible.
743 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
745 return file
->f_pos
= 0;
749 * The memory devices use the full 32/64 bits of the offset, and so we cannot
750 * check against negative addresses: they are ok. The return value is weird,
751 * though, in that case (0).
753 * also note that seeking relative to the "end of file" isn't supported:
754 * it has no meaning, so it returns -EINVAL.
756 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
760 mutex_lock(&file_inode(file
)->i_mutex
);
763 offset
+= file
->f_pos
;
765 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
766 if ((unsigned long long)offset
>= ~0xFFFULL
) {
770 file
->f_pos
= offset
;
772 force_successful_syscall_return();
777 mutex_unlock(&file_inode(file
)->i_mutex
);
781 static int open_port(struct inode
*inode
, struct file
*filp
)
783 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
786 #define zero_lseek null_lseek
787 #define full_lseek null_lseek
788 #define write_zero write_null
789 #define read_full read_zero
790 #define aio_write_zero aio_write_null
791 #define open_mem open_port
792 #define open_kmem open_mem
793 #define open_oldmem open_mem
795 static const struct file_operations mem_fops
= {
796 .llseek
= memory_lseek
,
801 .get_unmapped_area
= get_unmapped_area_mem
,
804 #ifdef CONFIG_DEVKMEM
805 static const struct file_operations kmem_fops
= {
806 .llseek
= memory_lseek
,
811 .get_unmapped_area
= get_unmapped_area_mem
,
815 static const struct file_operations null_fops
= {
816 .llseek
= null_lseek
,
819 .aio_read
= aio_read_null
,
820 .aio_write
= aio_write_null
,
821 .splice_write
= splice_write_null
,
824 #ifdef CONFIG_DEVPORT
825 static const struct file_operations port_fops
= {
826 .llseek
= memory_lseek
,
833 static const struct file_operations zero_fops
= {
834 .llseek
= zero_lseek
,
837 .aio_read
= aio_read_zero
,
838 .aio_write
= aio_write_zero
,
843 * capabilities for /dev/zero
844 * - permits private mappings, "copies" are taken of the source of zeros
845 * - no writeback happens
847 static struct backing_dev_info zero_bdi
= {
849 .capabilities
= BDI_CAP_MAP_COPY
| BDI_CAP_NO_ACCT_AND_WRITEBACK
,
852 static const struct file_operations full_fops
= {
853 .llseek
= full_lseek
,
858 #ifdef CONFIG_CRASH_DUMP
859 static const struct file_operations oldmem_fops
= {
862 .llseek
= default_llseek
,
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
},
885 [11] = { "kmsg", 0644, &kmsg_fops
, NULL
},
887 #ifdef CONFIG_CRASH_DUMP
888 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
892 static int memory_open(struct inode
*inode
, struct file
*filp
)
895 const struct memdev
*dev
;
897 minor
= iminor(inode
);
898 if (minor
>= ARRAY_SIZE(devlist
))
901 dev
= &devlist
[minor
];
905 filp
->f_op
= dev
->fops
;
907 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
909 /* Is /dev/mem or /dev/kmem ? */
910 if (dev
->dev_info
== &directly_mappable_cdev_bdi
)
911 filp
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
914 return dev
->fops
->open(inode
, filp
);
919 static const struct file_operations memory_fops
= {
921 .llseek
= noop_llseek
,
924 static char *mem_devnode(struct device
*dev
, umode_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 if (IS_ERR(mem_class
))
947 return PTR_ERR(mem_class
);
949 mem_class
->devnode
= mem_devnode
;
950 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
951 if (!devlist
[minor
].name
)
957 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
960 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
961 NULL
, devlist
[minor
].name
);
967 fs_initcall(chr_dev_init
);