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>
31 #include <linux/module.h>
33 #include <linux/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 range_is_allowed(unsigned long pfn
, unsigned long size
)
66 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
71 if (!devmem_is_allowed(pfn
))
79 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
85 #ifndef unxlate_dev_mem_ptr
86 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
87 void __weak
unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
93 * This funcion reads the *physical* memory. The f_pos points directly to the
96 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
97 size_t count
, loff_t
*ppos
)
99 phys_addr_t p
= *ppos
;
106 if (!valid_phys_addr_range(p
, count
))
109 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
110 /* we don't have page 0 mapped on sparc and m68k.. */
112 sz
= size_inside_page(p
, count
);
114 if (clear_user(buf
, sz
))
125 unsigned long remaining
;
127 sz
= size_inside_page(p
, count
);
129 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
))
133 * On ia64 if a page has been mapped somewhere as uncached, then
134 * it must also be accessed uncached by the kernel or data
135 * corruption may occur.
137 ptr
= xlate_dev_mem_ptr(p
);
141 remaining
= copy_to_user(buf
, ptr
, sz
);
142 unxlate_dev_mem_ptr(p
, ptr
);
156 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
157 size_t count
, loff_t
*ppos
)
159 phys_addr_t p
= *ppos
;
161 unsigned long copied
;
167 if (!valid_phys_addr_range(p
, count
))
172 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
173 /* we don't have page 0 mapped on sparc and m68k.. */
175 sz
= size_inside_page(p
, count
);
176 /* Hmm. Do something? */
185 sz
= size_inside_page(p
, count
);
187 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
191 * On ia64 if a page has been mapped somewhere as uncached, then
192 * it must also be accessed uncached by the kernel or data
193 * corruption may occur.
195 ptr
= xlate_dev_mem_ptr(p
);
202 copied
= copy_from_user(ptr
, buf
, sz
);
203 unxlate_dev_mem_ptr(p
, ptr
);
205 written
+= sz
- copied
;
221 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
222 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
227 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
230 * Architectures vary in how they handle caching for addresses
231 * outside of main memory.
234 #ifdef pgprot_noncached
235 static int uncached_access(struct file
*file
, phys_addr_t addr
)
237 #if defined(CONFIG_IA64)
239 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
242 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
243 #elif defined(CONFIG_MIPS)
245 extern int __uncached_access(struct file
*file
,
248 return __uncached_access(file
, addr
);
252 * Accessing memory above the top the kernel knows about or through a
254 * that was marked O_DSYNC will be done non-cached.
256 if (file
->f_flags
& O_DSYNC
)
258 return addr
>= __pa(high_memory
);
263 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
264 unsigned long size
, pgprot_t vma_prot
)
266 #ifdef pgprot_noncached
267 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
269 if (uncached_access(file
, offset
))
270 return pgprot_noncached(vma_prot
);
277 static unsigned long get_unmapped_area_mem(struct file
*file
,
283 if (!valid_mmap_phys_addr_range(pgoff
, len
))
284 return (unsigned long) -EINVAL
;
285 return pgoff
<< PAGE_SHIFT
;
288 /* permit direct mmap, for read, write or exec */
289 static unsigned memory_mmap_capabilities(struct file
*file
)
291 return NOMMU_MAP_DIRECT
|
292 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
295 static unsigned zero_mmap_capabilities(struct file
*file
)
297 return NOMMU_MAP_COPY
;
300 /* can't do an in-place private mapping if there's no MMU */
301 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
303 return vma
->vm_flags
& VM_MAYSHARE
;
307 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
313 static const struct vm_operations_struct mmap_mem_ops
= {
314 #ifdef CONFIG_HAVE_IOREMAP_PROT
315 .access
= generic_access_phys
319 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
321 size_t size
= vma
->vm_end
- vma
->vm_start
;
323 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
326 if (!private_mapping_ok(vma
))
329 if (!range_is_allowed(vma
->vm_pgoff
, size
))
332 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
336 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
340 vma
->vm_ops
= &mmap_mem_ops
;
342 /* Remap-pfn-range will mark the range VM_IO */
343 if (remap_pfn_range(vma
,
347 vma
->vm_page_prot
)) {
353 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
357 /* Turn a kernel-virtual address into a physical page frame */
358 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
361 * RED-PEN: on some architectures there is more mapped memory than
362 * available in mem_map which pfn_valid checks for. Perhaps should add a
365 * RED-PEN: vmalloc is not supported right now.
371 return mmap_mem(file
, vma
);
375 * This function reads the *virtual* memory as seen by the kernel.
377 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
378 size_t count
, loff_t
*ppos
)
380 unsigned long p
= *ppos
;
381 ssize_t low_count
, read
, sz
;
382 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
386 if (p
< (unsigned long) high_memory
) {
388 if (count
> (unsigned long)high_memory
- p
)
389 low_count
= (unsigned long)high_memory
- p
;
391 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
392 /* we don't have page 0 mapped on sparc and m68k.. */
393 if (p
< PAGE_SIZE
&& low_count
> 0) {
394 sz
= size_inside_page(p
, low_count
);
395 if (clear_user(buf
, sz
))
404 while (low_count
> 0) {
405 sz
= size_inside_page(p
, low_count
);
408 * On ia64 if a page has been mapped somewhere as
409 * uncached, then it must also be accessed uncached
410 * by the kernel or data corruption may occur
412 kbuf
= xlate_dev_kmem_ptr((void *)p
);
413 if (!virt_addr_valid(kbuf
))
416 if (copy_to_user(buf
, kbuf
, sz
))
427 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
431 sz
= size_inside_page(p
, count
);
432 if (!is_vmalloc_or_module_addr((void *)p
)) {
436 sz
= vread(kbuf
, (char *)p
, sz
);
439 if (copy_to_user(buf
, kbuf
, sz
)) {
448 free_page((unsigned long)kbuf
);
451 return read
? read
: err
;
455 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
456 size_t count
, loff_t
*ppos
)
459 unsigned long copied
;
462 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
463 /* we don't have page 0 mapped on sparc and m68k.. */
465 sz
= size_inside_page(p
, count
);
466 /* Hmm. Do something? */
477 sz
= size_inside_page(p
, count
);
480 * On ia64 if a page has been mapped somewhere as uncached, then
481 * it must also be accessed uncached by the kernel or data
482 * corruption may occur.
484 ptr
= xlate_dev_kmem_ptr((void *)p
);
485 if (!virt_addr_valid(ptr
))
488 copied
= copy_from_user(ptr
, buf
, sz
);
490 written
+= sz
- copied
;
506 * This function writes to the *virtual* memory as seen by the kernel.
508 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
509 size_t count
, loff_t
*ppos
)
511 unsigned long p
= *ppos
;
514 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
517 if (p
< (unsigned long) high_memory
) {
518 unsigned long to_write
= min_t(unsigned long, count
,
519 (unsigned long)high_memory
- p
);
520 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
521 if (wrote
!= to_write
)
529 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
531 return wrote
? wrote
: -ENOMEM
;
533 unsigned long sz
= size_inside_page(p
, count
);
536 if (!is_vmalloc_or_module_addr((void *)p
)) {
540 n
= copy_from_user(kbuf
, buf
, sz
);
545 vwrite(kbuf
, (char *)p
, sz
);
551 free_page((unsigned long)kbuf
);
555 return virtr
+ wrote
? : err
;
558 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
559 size_t count
, loff_t
*ppos
)
561 unsigned long i
= *ppos
;
562 char __user
*tmp
= buf
;
564 if (!access_ok(VERIFY_WRITE
, buf
, count
))
566 while (count
-- > 0 && i
< 65536) {
567 if (__put_user(inb(i
), tmp
) < 0)
576 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
577 size_t count
, loff_t
*ppos
)
579 unsigned long i
= *ppos
;
580 const char __user
*tmp
= buf
;
582 if (secure_modules())
585 if (!access_ok(VERIFY_READ
, buf
, count
))
587 while (count
-- > 0 && i
< 65536) {
590 if (__get_user(c
, tmp
)) {
603 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
604 size_t count
, loff_t
*ppos
)
609 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
610 size_t count
, loff_t
*ppos
)
615 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
620 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
622 size_t count
= iov_iter_count(from
);
623 iov_iter_advance(from
, count
);
627 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
628 struct splice_desc
*sd
)
633 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
634 loff_t
*ppos
, size_t len
, unsigned int flags
)
636 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
639 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
643 while (iov_iter_count(iter
)) {
644 size_t chunk
= iov_iter_count(iter
), n
;
646 if (chunk
> PAGE_SIZE
)
647 chunk
= PAGE_SIZE
; /* Just for latency reasons */
648 n
= iov_iter_zero(chunk
, iter
);
649 if (!n
&& iov_iter_count(iter
))
650 return written
? written
: -EFAULT
;
652 if (signal_pending(current
))
653 return written
? written
: -ERESTARTSYS
;
659 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
664 if (vma
->vm_flags
& VM_SHARED
)
665 return shmem_zero_setup(vma
);
669 static unsigned long get_unmapped_area_zero(struct file
*file
,
670 unsigned long addr
, unsigned long len
,
671 unsigned long pgoff
, unsigned long flags
)
674 if (flags
& MAP_SHARED
) {
676 * mmap_zero() will call shmem_zero_setup() to create a file,
677 * so use shmem's get_unmapped_area in case it can be huge;
678 * and pass NULL for file as in mmap.c's get_unmapped_area(),
679 * so as not to confuse shmem with our handle on "/dev/zero".
681 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
684 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
685 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
691 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
692 size_t count
, loff_t
*ppos
)
698 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
699 * can fopen() both devices with "a" now. This was previously impossible.
702 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
704 return file
->f_pos
= 0;
708 * The memory devices use the full 32/64 bits of the offset, and so we cannot
709 * check against negative addresses: they are ok. The return value is weird,
710 * though, in that case (0).
712 * also note that seeking relative to the "end of file" isn't supported:
713 * it has no meaning, so it returns -EINVAL.
715 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
719 inode_lock(file_inode(file
));
722 offset
+= file
->f_pos
;
724 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
725 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
729 file
->f_pos
= offset
;
731 force_successful_syscall_return();
736 inode_unlock(file_inode(file
));
740 static int open_port(struct inode
*inode
, struct file
*filp
)
742 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
745 #define zero_lseek null_lseek
746 #define full_lseek null_lseek
747 #define write_zero write_null
748 #define write_iter_zero write_iter_null
749 #define open_mem open_port
750 #define open_kmem open_mem
752 static const struct file_operations __maybe_unused mem_fops
= {
753 .llseek
= memory_lseek
,
759 .get_unmapped_area
= get_unmapped_area_mem
,
760 .mmap_capabilities
= memory_mmap_capabilities
,
764 static const struct file_operations __maybe_unused kmem_fops
= {
765 .llseek
= memory_lseek
,
771 .get_unmapped_area
= get_unmapped_area_mem
,
772 .mmap_capabilities
= memory_mmap_capabilities
,
776 static const struct file_operations null_fops
= {
777 .llseek
= null_lseek
,
780 .read_iter
= read_iter_null
,
781 .write_iter
= write_iter_null
,
782 .splice_write
= splice_write_null
,
785 static const struct file_operations __maybe_unused port_fops
= {
786 .llseek
= memory_lseek
,
792 static const struct file_operations zero_fops
= {
793 .llseek
= zero_lseek
,
795 .read_iter
= read_iter_zero
,
796 .write_iter
= write_iter_zero
,
798 .get_unmapped_area
= get_unmapped_area_zero
,
800 .mmap_capabilities
= zero_mmap_capabilities
,
804 static const struct file_operations full_fops
= {
805 .llseek
= full_lseek
,
806 .read_iter
= read_iter_zero
,
810 static const struct memdev
{
813 const struct file_operations
*fops
;
817 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
819 #ifdef CONFIG_DEVKMEM
820 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
822 [3] = { "null", 0666, &null_fops
, 0 },
823 #ifdef CONFIG_DEVPORT
824 [4] = { "port", 0, &port_fops
, 0 },
826 [5] = { "zero", 0666, &zero_fops
, 0 },
827 [7] = { "full", 0666, &full_fops
, 0 },
828 [8] = { "random", 0666, &random_fops
, 0 },
829 [9] = { "urandom", 0666, &urandom_fops
, 0 },
831 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
835 static int memory_open(struct inode
*inode
, struct file
*filp
)
838 const struct memdev
*dev
;
840 minor
= iminor(inode
);
841 if (minor
>= ARRAY_SIZE(devlist
))
844 dev
= &devlist
[minor
];
848 filp
->f_op
= dev
->fops
;
849 filp
->f_mode
|= dev
->fmode
;
852 return dev
->fops
->open(inode
, filp
);
857 static const struct file_operations memory_fops
= {
859 .llseek
= noop_llseek
,
862 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
864 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
865 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
869 static struct class *mem_class
;
871 static int __init
chr_dev_init(void)
875 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
876 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
878 mem_class
= class_create(THIS_MODULE
, "mem");
879 if (IS_ERR(mem_class
))
880 return PTR_ERR(mem_class
);
882 mem_class
->devnode
= mem_devnode
;
883 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
884 if (!devlist
[minor
].name
)
890 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
893 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
894 NULL
, devlist
[minor
].name
);
900 fs_initcall(chr_dev_init
);