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/backing-dev.h>
25 #include <linux/shmem_fs.h>
26 #include <linux/splice.h>
27 #include <linux/pfn.h>
28 #include <linux/export.h>
30 #include <linux/uio.h>
32 #include <linux/uaccess.h>
35 # include <linux/efi.h>
38 #define DEVPORT_MINOR 4
40 static inline unsigned long size_inside_page(unsigned long start
,
45 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
50 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
51 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
53 return addr
+ count
<= __pa(high_memory
);
56 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
62 #ifdef CONFIG_STRICT_DEVMEM
63 static inline int page_is_allowed(unsigned long pfn
)
65 return devmem_is_allowed(pfn
);
67 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
69 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
74 if (!devmem_is_allowed(pfn
))
82 static inline int page_is_allowed(unsigned long pfn
)
86 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
92 #ifndef unxlate_dev_mem_ptr
93 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
94 void __weak
unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
100 * This funcion reads the *physical* memory. The f_pos points directly to the
103 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
104 size_t count
, loff_t
*ppos
)
106 phys_addr_t p
= *ppos
;
113 if (!valid_phys_addr_range(p
, count
))
116 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
117 /* we don't have page 0 mapped on sparc and m68k.. */
119 sz
= size_inside_page(p
, count
);
121 if (clear_user(buf
, sz
))
132 unsigned long remaining
;
135 sz
= size_inside_page(p
, count
);
137 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
141 /* Show zeros for restricted memory. */
142 remaining
= clear_user(buf
, sz
);
145 * On ia64 if a page has been mapped somewhere as
146 * uncached, then it must also be accessed uncached
147 * by the kernel or data corruption may occur.
149 ptr
= xlate_dev_mem_ptr(p
);
153 remaining
= copy_to_user(buf
, ptr
, sz
);
155 unxlate_dev_mem_ptr(p
, ptr
);
171 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
172 size_t count
, loff_t
*ppos
)
174 phys_addr_t p
= *ppos
;
176 unsigned long copied
;
182 if (kernel_is_locked_down())
185 if (!valid_phys_addr_range(p
, count
))
190 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
191 /* we don't have page 0 mapped on sparc and m68k.. */
193 sz
= size_inside_page(p
, count
);
194 /* Hmm. Do something? */
205 sz
= size_inside_page(p
, count
);
207 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
211 /* Skip actual writing when a page is marked as restricted. */
214 * On ia64 if a page has been mapped somewhere as
215 * uncached, then it must also be accessed uncached
216 * by the kernel or data corruption may occur.
218 ptr
= xlate_dev_mem_ptr(p
);
225 copied
= copy_from_user(ptr
, buf
, sz
);
226 unxlate_dev_mem_ptr(p
, ptr
);
228 written
+= sz
- copied
;
245 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
246 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
251 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
254 * Architectures vary in how they handle caching for addresses
255 * outside of main memory.
258 #ifdef pgprot_noncached
259 static int uncached_access(struct file
*file
, phys_addr_t addr
)
261 #if defined(CONFIG_IA64)
263 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
266 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
267 #elif defined(CONFIG_MIPS)
269 extern int __uncached_access(struct file
*file
,
272 return __uncached_access(file
, addr
);
276 * Accessing memory above the top the kernel knows about or through a
278 * that was marked O_DSYNC will be done non-cached.
280 if (file
->f_flags
& O_DSYNC
)
282 return addr
>= __pa(high_memory
);
287 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
288 unsigned long size
, pgprot_t vma_prot
)
290 #ifdef pgprot_noncached
291 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
293 if (uncached_access(file
, offset
))
294 return pgprot_noncached(vma_prot
);
301 static unsigned long get_unmapped_area_mem(struct file
*file
,
307 if (!valid_mmap_phys_addr_range(pgoff
, len
))
308 return (unsigned long) -EINVAL
;
309 return pgoff
<< PAGE_SHIFT
;
312 /* permit direct mmap, for read, write or exec */
313 static unsigned memory_mmap_capabilities(struct file
*file
)
315 return NOMMU_MAP_DIRECT
|
316 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
319 static unsigned zero_mmap_capabilities(struct file
*file
)
321 return NOMMU_MAP_COPY
;
324 /* can't do an in-place private mapping if there's no MMU */
325 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
327 return vma
->vm_flags
& VM_MAYSHARE
;
331 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
337 static const struct vm_operations_struct mmap_mem_ops
= {
338 #ifdef CONFIG_HAVE_IOREMAP_PROT
339 .access
= generic_access_phys
343 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
345 size_t size
= vma
->vm_end
- vma
->vm_start
;
346 phys_addr_t offset
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
348 /* It's illegal to wrap around the end of the physical address space. */
349 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
352 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
355 if (!private_mapping_ok(vma
))
358 if (!range_is_allowed(vma
->vm_pgoff
, size
))
361 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
365 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
369 vma
->vm_ops
= &mmap_mem_ops
;
371 /* Remap-pfn-range will mark the range VM_IO */
372 if (remap_pfn_range(vma
,
376 vma
->vm_page_prot
)) {
382 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
386 /* Turn a kernel-virtual address into a physical page frame */
387 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
390 * RED-PEN: on some architectures there is more mapped memory than
391 * available in mem_map which pfn_valid checks for. Perhaps should add a
394 * RED-PEN: vmalloc is not supported right now.
400 return mmap_mem(file
, vma
);
404 * This function reads the *virtual* memory as seen by the kernel.
406 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
407 size_t count
, loff_t
*ppos
)
409 unsigned long p
= *ppos
;
410 ssize_t low_count
, read
, sz
;
411 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
415 if (p
< (unsigned long) high_memory
) {
417 if (count
> (unsigned long)high_memory
- p
)
418 low_count
= (unsigned long)high_memory
- p
;
420 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
421 /* we don't have page 0 mapped on sparc and m68k.. */
422 if (p
< PAGE_SIZE
&& low_count
> 0) {
423 sz
= size_inside_page(p
, low_count
);
424 if (clear_user(buf
, sz
))
433 while (low_count
> 0) {
434 sz
= size_inside_page(p
, low_count
);
437 * On ia64 if a page has been mapped somewhere as
438 * uncached, then it must also be accessed uncached
439 * by the kernel or data corruption may occur
441 kbuf
= xlate_dev_kmem_ptr((void *)p
);
442 if (!virt_addr_valid(kbuf
))
445 if (copy_to_user(buf
, kbuf
, sz
))
456 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
460 sz
= size_inside_page(p
, count
);
461 if (!is_vmalloc_or_module_addr((void *)p
)) {
465 sz
= vread(kbuf
, (char *)p
, sz
);
468 if (copy_to_user(buf
, kbuf
, sz
)) {
477 free_page((unsigned long)kbuf
);
480 return read
? read
: err
;
484 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
485 size_t count
, loff_t
*ppos
)
488 unsigned long copied
;
491 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
492 /* we don't have page 0 mapped on sparc and m68k.. */
494 sz
= size_inside_page(p
, count
);
495 /* Hmm. Do something? */
506 sz
= size_inside_page(p
, count
);
509 * On ia64 if a page has been mapped somewhere as uncached, then
510 * it must also be accessed uncached by the kernel or data
511 * corruption may occur.
513 ptr
= xlate_dev_kmem_ptr((void *)p
);
514 if (!virt_addr_valid(ptr
))
517 copied
= copy_from_user(ptr
, buf
, sz
);
519 written
+= sz
- copied
;
535 * This function writes to the *virtual* memory as seen by the kernel.
537 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
538 size_t count
, loff_t
*ppos
)
540 unsigned long p
= *ppos
;
543 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
546 if (kernel_is_locked_down())
549 if (p
< (unsigned long) high_memory
) {
550 unsigned long to_write
= min_t(unsigned long, count
,
551 (unsigned long)high_memory
- p
);
552 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
553 if (wrote
!= to_write
)
561 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
563 return wrote
? wrote
: -ENOMEM
;
565 unsigned long sz
= size_inside_page(p
, count
);
568 if (!is_vmalloc_or_module_addr((void *)p
)) {
572 n
= copy_from_user(kbuf
, buf
, sz
);
577 vwrite(kbuf
, (char *)p
, sz
);
583 free_page((unsigned long)kbuf
);
587 return virtr
+ wrote
? : err
;
590 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
591 size_t count
, loff_t
*ppos
)
593 unsigned long i
= *ppos
;
594 char __user
*tmp
= buf
;
596 if (!access_ok(VERIFY_WRITE
, buf
, count
))
598 while (count
-- > 0 && i
< 65536) {
599 if (__put_user(inb(i
), tmp
) < 0)
608 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
609 size_t count
, loff_t
*ppos
)
611 unsigned long i
= *ppos
;
612 const char __user
*tmp
= buf
;
614 if (!access_ok(VERIFY_READ
, buf
, count
))
616 while (count
-- > 0 && i
< 65536) {
619 if (__get_user(c
, tmp
)) {
632 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
633 size_t count
, loff_t
*ppos
)
638 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
639 size_t count
, loff_t
*ppos
)
644 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
649 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
651 size_t count
= iov_iter_count(from
);
652 iov_iter_advance(from
, count
);
656 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
657 struct splice_desc
*sd
)
662 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
663 loff_t
*ppos
, size_t len
, unsigned int flags
)
665 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
668 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
672 while (iov_iter_count(iter
)) {
673 size_t chunk
= iov_iter_count(iter
), n
;
675 if (chunk
> PAGE_SIZE
)
676 chunk
= PAGE_SIZE
; /* Just for latency reasons */
677 n
= iov_iter_zero(chunk
, iter
);
678 if (!n
&& iov_iter_count(iter
))
679 return written
? written
: -EFAULT
;
681 if (signal_pending(current
))
682 return written
? written
: -ERESTARTSYS
;
688 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
693 if (vma
->vm_flags
& VM_SHARED
)
694 return shmem_zero_setup(vma
);
698 static unsigned long get_unmapped_area_zero(struct file
*file
,
699 unsigned long addr
, unsigned long len
,
700 unsigned long pgoff
, unsigned long flags
)
703 if (flags
& MAP_SHARED
) {
705 * mmap_zero() will call shmem_zero_setup() to create a file,
706 * so use shmem's get_unmapped_area in case it can be huge;
707 * and pass NULL for file as in mmap.c's get_unmapped_area(),
708 * so as not to confuse shmem with our handle on "/dev/zero".
710 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
713 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
714 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
720 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
721 size_t count
, loff_t
*ppos
)
727 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
728 * can fopen() both devices with "a" now. This was previously impossible.
731 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
733 return file
->f_pos
= 0;
737 * The memory devices use the full 32/64 bits of the offset, and so we cannot
738 * check against negative addresses: they are ok. The return value is weird,
739 * though, in that case (0).
741 * also note that seeking relative to the "end of file" isn't supported:
742 * it has no meaning, so it returns -EINVAL.
744 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
748 inode_lock(file_inode(file
));
751 offset
+= file
->f_pos
;
753 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
754 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
758 file
->f_pos
= offset
;
760 force_successful_syscall_return();
765 inode_unlock(file_inode(file
));
769 static int open_port(struct inode
*inode
, struct file
*filp
)
771 if (kernel_is_locked_down())
773 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
776 #define zero_lseek null_lseek
777 #define full_lseek null_lseek
778 #define write_zero write_null
779 #define write_iter_zero write_iter_null
780 #define open_mem open_port
781 #define open_kmem open_mem
783 static const struct file_operations __maybe_unused mem_fops
= {
784 .llseek
= memory_lseek
,
790 .get_unmapped_area
= get_unmapped_area_mem
,
791 .mmap_capabilities
= memory_mmap_capabilities
,
795 static const struct file_operations __maybe_unused kmem_fops
= {
796 .llseek
= memory_lseek
,
802 .get_unmapped_area
= get_unmapped_area_mem
,
803 .mmap_capabilities
= memory_mmap_capabilities
,
807 static const struct file_operations null_fops
= {
808 .llseek
= null_lseek
,
811 .read_iter
= read_iter_null
,
812 .write_iter
= write_iter_null
,
813 .splice_write
= splice_write_null
,
816 static const struct file_operations __maybe_unused port_fops
= {
817 .llseek
= memory_lseek
,
823 static const struct file_operations zero_fops
= {
824 .llseek
= zero_lseek
,
826 .read_iter
= read_iter_zero
,
827 .write_iter
= write_iter_zero
,
829 .get_unmapped_area
= get_unmapped_area_zero
,
831 .mmap_capabilities
= zero_mmap_capabilities
,
835 static const struct file_operations full_fops
= {
836 .llseek
= full_lseek
,
837 .read_iter
= read_iter_zero
,
841 static const struct memdev
{
844 const struct file_operations
*fops
;
848 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
850 #ifdef CONFIG_DEVKMEM
851 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
853 [3] = { "null", 0666, &null_fops
, 0 },
854 #ifdef CONFIG_DEVPORT
855 [4] = { "port", 0, &port_fops
, 0 },
857 [5] = { "zero", 0666, &zero_fops
, 0 },
858 [7] = { "full", 0666, &full_fops
, 0 },
859 [8] = { "random", 0666, &random_fops
, 0 },
860 [9] = { "urandom", 0666, &urandom_fops
, 0 },
862 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
866 static int memory_open(struct inode
*inode
, struct file
*filp
)
869 const struct memdev
*dev
;
871 minor
= iminor(inode
);
872 if (minor
>= ARRAY_SIZE(devlist
))
875 dev
= &devlist
[minor
];
879 filp
->f_op
= dev
->fops
;
880 filp
->f_mode
|= dev
->fmode
;
883 return dev
->fops
->open(inode
, filp
);
888 static const struct file_operations memory_fops
= {
890 .llseek
= noop_llseek
,
893 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
895 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
896 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
900 static struct class *mem_class
;
902 static int __init
chr_dev_init(void)
906 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
907 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
909 mem_class
= class_create(THIS_MODULE
, "mem");
910 if (IS_ERR(mem_class
))
911 return PTR_ERR(mem_class
);
913 mem_class
->devnode
= mem_devnode
;
914 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
915 if (!devlist
[minor
].name
)
921 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
924 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
925 NULL
, devlist
[minor
].name
);
931 fs_initcall(chr_dev_init
);