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 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
64 #ifdef CONFIG_STRICT_DEVMEM
65 static inline int page_is_allowed(unsigned long pfn
)
67 return devmem_is_allowed(pfn
);
69 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
71 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
76 if (!devmem_is_allowed(pfn
))
84 static inline int page_is_allowed(unsigned long pfn
)
88 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
96 void __weak
unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
101 * This funcion reads the *physical* memory. The f_pos points directly to the
104 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
105 size_t count
, loff_t
*ppos
)
107 phys_addr_t p
= *ppos
;
111 if (!valid_phys_addr_range(p
, count
))
114 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
115 /* we don't have page 0 mapped on sparc and m68k.. */
117 sz
= size_inside_page(p
, count
);
119 if (clear_user(buf
, sz
))
130 unsigned long remaining
;
133 sz
= size_inside_page(p
, count
);
135 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
139 /* Show zeros for restricted memory. */
140 remaining
= clear_user(buf
, sz
);
143 * On ia64 if a page has been mapped somewhere as
144 * uncached, then it must also be accessed uncached
145 * by the kernel or data corruption may occur.
147 ptr
= xlate_dev_mem_ptr(p
);
151 remaining
= copy_to_user(buf
, ptr
, sz
);
153 unxlate_dev_mem_ptr(p
, ptr
);
169 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
170 size_t count
, loff_t
*ppos
)
172 phys_addr_t p
= *ppos
;
174 unsigned long copied
;
177 if (!valid_phys_addr_range(p
, count
))
182 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
183 /* we don't have page 0 mapped on sparc and m68k.. */
185 sz
= size_inside_page(p
, count
);
186 /* Hmm. Do something? */
197 sz
= size_inside_page(p
, count
);
199 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
203 /* Skip actual writing when a page is marked as restricted. */
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
);
217 copied
= copy_from_user(ptr
, buf
, sz
);
218 unxlate_dev_mem_ptr(p
, ptr
);
220 written
+= sz
- copied
;
236 #endif /* CONFIG_DEVMEM */
238 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
240 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
241 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
246 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
249 * Architectures vary in how they handle caching for addresses
250 * outside of main memory.
253 #ifdef pgprot_noncached
254 static int uncached_access(struct file
*file
, phys_addr_t addr
)
256 #if defined(CONFIG_IA64)
258 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
261 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
262 #elif defined(CONFIG_MIPS)
264 extern int __uncached_access(struct file
*file
,
267 return __uncached_access(file
, addr
);
271 * Accessing memory above the top the kernel knows about or through a
273 * that was marked O_DSYNC will be done non-cached.
275 if (file
->f_flags
& O_DSYNC
)
277 return addr
>= __pa(high_memory
);
282 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
283 unsigned long size
, pgprot_t vma_prot
)
285 #ifdef pgprot_noncached
286 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
288 if (uncached_access(file
, offset
))
289 return pgprot_noncached(vma_prot
);
296 static unsigned long get_unmapped_area_mem(struct file
*file
,
302 if (!valid_mmap_phys_addr_range(pgoff
, len
))
303 return (unsigned long) -EINVAL
;
304 return pgoff
<< PAGE_SHIFT
;
307 /* can't do an in-place private mapping if there's no MMU */
308 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
310 return vma
->vm_flags
& VM_MAYSHARE
;
313 #define get_unmapped_area_mem NULL
315 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
321 static const struct vm_operations_struct mmap_mem_ops
= {
322 #ifdef CONFIG_HAVE_IOREMAP_PROT
323 .access
= generic_access_phys
327 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
329 size_t size
= vma
->vm_end
- vma
->vm_start
;
331 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
334 if (!private_mapping_ok(vma
))
337 if (!range_is_allowed(vma
->vm_pgoff
, size
))
340 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
344 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
348 vma
->vm_ops
= &mmap_mem_ops
;
350 /* Remap-pfn-range will mark the range VM_IO */
351 if (remap_pfn_range(vma
,
355 vma
->vm_page_prot
)) {
360 #endif /* CONFIG_DEVMEM */
362 #ifdef CONFIG_DEVKMEM
363 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
367 /* Turn a kernel-virtual address into a physical page frame */
368 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
371 * RED-PEN: on some architectures there is more mapped memory than
372 * available in mem_map which pfn_valid checks for. Perhaps should add a
375 * RED-PEN: vmalloc is not supported right now.
381 return mmap_mem(file
, vma
);
385 #ifdef CONFIG_CRASH_DUMP
387 * Read memory corresponding to the old kernel.
389 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
390 size_t count
, loff_t
*ppos
)
392 unsigned long pfn
, offset
;
393 size_t read
= 0, csize
;
397 pfn
= *ppos
/ PAGE_SIZE
;
398 if (pfn
> saved_max_pfn
)
401 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
402 if (count
> PAGE_SIZE
- offset
)
403 csize
= PAGE_SIZE
- offset
;
407 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
419 #ifdef CONFIG_DEVKMEM
421 * This function reads the *virtual* memory as seen by the kernel.
423 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
424 size_t count
, loff_t
*ppos
)
426 unsigned long p
= *ppos
;
427 ssize_t low_count
, read
, sz
;
428 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
432 if (p
< (unsigned long) high_memory
) {
434 if (count
> (unsigned long)high_memory
- p
)
435 low_count
= (unsigned long)high_memory
- p
;
437 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
438 /* we don't have page 0 mapped on sparc and m68k.. */
439 if (p
< PAGE_SIZE
&& low_count
> 0) {
440 sz
= size_inside_page(p
, low_count
);
441 if (clear_user(buf
, sz
))
450 while (low_count
> 0) {
451 sz
= size_inside_page(p
, low_count
);
454 * On ia64 if a page has been mapped somewhere as
455 * uncached, then it must also be accessed uncached
456 * by the kernel or data corruption may occur
458 kbuf
= xlate_dev_kmem_ptr((char *)p
);
460 if (copy_to_user(buf
, kbuf
, sz
))
471 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
475 sz
= size_inside_page(p
, count
);
476 if (!is_vmalloc_or_module_addr((void *)p
)) {
480 sz
= vread(kbuf
, (char *)p
, sz
);
483 if (copy_to_user(buf
, kbuf
, sz
)) {
492 free_page((unsigned long)kbuf
);
495 return read
? read
: err
;
499 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
500 size_t count
, loff_t
*ppos
)
503 unsigned long copied
;
506 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
507 /* we don't have page 0 mapped on sparc and m68k.. */
509 sz
= size_inside_page(p
, count
);
510 /* Hmm. Do something? */
521 sz
= size_inside_page(p
, count
);
524 * On ia64 if a page has been mapped somewhere as uncached, then
525 * it must also be accessed uncached by the kernel or data
526 * corruption may occur.
528 ptr
= xlate_dev_kmem_ptr((char *)p
);
530 copied
= copy_from_user(ptr
, buf
, sz
);
532 written
+= sz
- copied
;
548 * This function writes to the *virtual* memory as seen by the kernel.
550 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
551 size_t count
, loff_t
*ppos
)
553 unsigned long p
= *ppos
;
556 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
559 if (p
< (unsigned long) high_memory
) {
560 unsigned long to_write
= min_t(unsigned long, count
,
561 (unsigned long)high_memory
- p
);
562 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
563 if (wrote
!= to_write
)
571 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
573 return wrote
? wrote
: -ENOMEM
;
575 unsigned long sz
= size_inside_page(p
, count
);
578 if (!is_vmalloc_or_module_addr((void *)p
)) {
582 n
= copy_from_user(kbuf
, buf
, sz
);
587 vwrite(kbuf
, (char *)p
, sz
);
593 free_page((unsigned long)kbuf
);
597 return virtr
+ wrote
? : err
;
601 #ifdef CONFIG_DEVPORT
602 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
603 size_t count
, loff_t
*ppos
)
605 unsigned long i
= *ppos
;
606 char __user
*tmp
= buf
;
608 if (!access_ok(VERIFY_WRITE
, buf
, count
))
610 while (count
-- > 0 && i
< 65536) {
611 if (__put_user(inb(i
), tmp
) < 0)
620 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
621 size_t count
, loff_t
*ppos
)
623 unsigned long i
= *ppos
;
624 const char __user
*tmp
= buf
;
626 if (!access_ok(VERIFY_READ
, buf
, count
))
628 while (count
-- > 0 && i
< 65536) {
630 if (__get_user(c
, tmp
)) {
644 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
645 size_t count
, loff_t
*ppos
)
650 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
651 size_t count
, loff_t
*ppos
)
656 static ssize_t
aio_read_null(struct kiocb
*iocb
, const struct iovec
*iov
,
657 unsigned long nr_segs
, loff_t pos
)
662 static ssize_t
aio_write_null(struct kiocb
*iocb
, const struct iovec
*iov
,
663 unsigned long nr_segs
, loff_t pos
)
665 return iov_length(iov
, nr_segs
);
668 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
669 struct splice_desc
*sd
)
674 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
675 loff_t
*ppos
, size_t len
, unsigned int flags
)
677 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
680 static ssize_t
read_zero(struct file
*file
, char __user
*buf
,
681 size_t count
, loff_t
*ppos
)
688 if (!access_ok(VERIFY_WRITE
, buf
, count
))
693 unsigned long unwritten
;
694 size_t chunk
= count
;
696 if (chunk
> PAGE_SIZE
)
697 chunk
= PAGE_SIZE
; /* Just for latency reasons */
698 unwritten
= __clear_user(buf
, chunk
);
699 written
+= chunk
- unwritten
;
702 if (signal_pending(current
))
703 return written
? written
: -ERESTARTSYS
;
708 return written
? written
: -EFAULT
;
711 static ssize_t
aio_read_zero(struct kiocb
*iocb
, const struct iovec
*iov
,
712 unsigned long nr_segs
, loff_t pos
)
718 for (i
= 0; i
< nr_segs
; i
++) {
719 ret
= read_zero(iocb
->ki_filp
, iov
[i
].iov_base
, iov
[i
].iov_len
,
726 return written
? written
: -EFAULT
;
729 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
734 if (vma
->vm_flags
& VM_SHARED
)
735 return shmem_zero_setup(vma
);
739 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
740 size_t count
, loff_t
*ppos
)
746 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
747 * can fopen() both devices with "a" now. This was previously impossible.
750 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
752 return file
->f_pos
= 0;
755 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
758 * The memory devices use the full 32/64 bits of the offset, and so we cannot
759 * check against negative addresses: they are ok. The return value is weird,
760 * though, in that case (0).
762 * also note that seeking relative to the "end of file" isn't supported:
763 * it has no meaning, so it returns -EINVAL.
765 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
769 mutex_lock(&file_inode(file
)->i_mutex
);
772 offset
+= file
->f_pos
;
774 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
775 if ((unsigned long long)offset
>= ~0xFFFULL
) {
779 file
->f_pos
= offset
;
781 force_successful_syscall_return();
786 mutex_unlock(&file_inode(file
)->i_mutex
);
792 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
793 static int open_port(struct inode
*inode
, struct file
*filp
)
795 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
799 #define zero_lseek null_lseek
800 #define full_lseek null_lseek
801 #define write_zero write_null
802 #define read_full read_zero
803 #define aio_write_zero aio_write_null
804 #define open_mem open_port
805 #define open_kmem open_mem
806 #define open_oldmem open_mem
809 static const struct file_operations mem_fops
= {
810 .llseek
= memory_lseek
,
815 .get_unmapped_area
= get_unmapped_area_mem
,
819 #ifdef CONFIG_DEVKMEM
820 static const struct file_operations kmem_fops
= {
821 .llseek
= memory_lseek
,
826 .get_unmapped_area
= get_unmapped_area_mem
,
830 static const struct file_operations null_fops
= {
831 .llseek
= null_lseek
,
834 .aio_read
= aio_read_null
,
835 .aio_write
= aio_write_null
,
836 .splice_write
= splice_write_null
,
839 #ifdef CONFIG_DEVPORT
840 static const struct file_operations port_fops
= {
841 .llseek
= memory_lseek
,
848 static const struct file_operations zero_fops
= {
849 .llseek
= zero_lseek
,
852 .aio_read
= aio_read_zero
,
853 .aio_write
= aio_write_zero
,
858 * capabilities for /dev/zero
859 * - permits private mappings, "copies" are taken of the source of zeros
860 * - no writeback happens
862 static struct backing_dev_info zero_bdi
= {
864 .capabilities
= BDI_CAP_MAP_COPY
| BDI_CAP_NO_ACCT_AND_WRITEBACK
,
867 static const struct file_operations full_fops
= {
868 .llseek
= full_lseek
,
873 #ifdef CONFIG_CRASH_DUMP
874 static const struct file_operations oldmem_fops
= {
877 .llseek
= default_llseek
,
881 static const struct memdev
{
884 const struct file_operations
*fops
;
885 struct backing_dev_info
*dev_info
;
888 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
890 #ifdef CONFIG_DEVKMEM
891 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
893 [3] = { "null", 0666, &null_fops
, NULL
},
894 #ifdef CONFIG_DEVPORT
895 [4] = { "port", 0, &port_fops
, NULL
},
897 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
898 [7] = { "full", 0666, &full_fops
, NULL
},
899 [8] = { "random", 0666, &random_fops
, NULL
},
900 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
902 [11] = { "kmsg", 0644, &kmsg_fops
, NULL
},
904 #ifdef CONFIG_CRASH_DUMP
905 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
909 static int memory_open(struct inode
*inode
, struct file
*filp
)
912 const struct memdev
*dev
;
914 minor
= iminor(inode
);
915 if (minor
>= ARRAY_SIZE(devlist
))
918 dev
= &devlist
[minor
];
922 filp
->f_op
= dev
->fops
;
924 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
926 /* Is /dev/mem or /dev/kmem ? */
927 if (dev
->dev_info
== &directly_mappable_cdev_bdi
)
928 filp
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
931 return dev
->fops
->open(inode
, filp
);
936 static const struct file_operations memory_fops
= {
938 .llseek
= noop_llseek
,
941 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
943 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
944 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
948 static struct class *mem_class
;
950 static int __init
chr_dev_init(void)
955 err
= bdi_init(&zero_bdi
);
959 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
960 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
962 mem_class
= class_create(THIS_MODULE
, "mem");
963 if (IS_ERR(mem_class
))
964 return PTR_ERR(mem_class
);
966 mem_class
->devnode
= mem_devnode
;
967 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
968 if (!devlist
[minor
].name
)
974 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
977 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
978 NULL
, devlist
[minor
].name
);
984 fs_initcall(chr_dev_init
);