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/splice.h>
26 #include <linux/pfn.h>
27 #include <linux/export.h>
29 #include <linux/uio.h>
31 #ifdef CONFIG_KNOX_KAP
32 #include <linux/knox_kap.h>
36 #include <linux/mst_ctrl.h>
39 #include <linux/uaccess.h>
42 # include <linux/efi.h>
45 #define DEVPORT_MINOR 4
47 static inline unsigned long size_inside_page(unsigned long start
,
52 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
57 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
58 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
60 return addr
+ count
<= __pa(high_memory
);
63 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
69 #ifdef CONFIG_STRICT_DEVMEM
70 static inline int page_is_allowed(unsigned long pfn
)
72 return devmem_is_allowed(pfn
);
74 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
76 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
81 if (!devmem_is_allowed(pfn
)) {
83 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
84 current
->comm
, from
, to
);
93 static inline int page_is_allowed(unsigned long pfn
)
97 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
103 #ifndef unxlate_dev_mem_ptr
104 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
105 void __weak
unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
111 * This funcion reads the *physical* memory. The f_pos points directly to the
114 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
115 size_t count
, loff_t
*ppos
)
117 phys_addr_t 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.. */
130 sz
= size_inside_page(p
, count
);
132 if (clear_user(buf
, sz
))
143 unsigned long remaining
;
146 sz
= size_inside_page(p
, count
);
148 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
152 /* Show zeros for restricted memory. */
153 remaining
= clear_user(buf
, sz
);
156 * On ia64 if a page has been mapped somewhere as
157 * uncached, then it must also be accessed uncached
158 * by the kernel or data corruption may occur.
160 ptr
= xlate_dev_mem_ptr(p
);
164 remaining
= copy_to_user(buf
, ptr
, sz
);
166 unxlate_dev_mem_ptr(p
, ptr
);
182 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
183 size_t count
, loff_t
*ppos
)
185 phys_addr_t p
= *ppos
;
187 unsigned long copied
;
193 if (!valid_phys_addr_range(p
, count
))
198 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
199 /* we don't have page 0 mapped on sparc and m68k.. */
201 sz
= size_inside_page(p
, count
);
202 /* Hmm. Do something? */
213 sz
= size_inside_page(p
, count
);
215 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
219 /* Skip actual writing when a page is marked as restricted. */
222 * On ia64 if a page has been mapped somewhere as
223 * uncached, then it must also be accessed uncached
224 * by the kernel or data corruption may occur.
226 ptr
= xlate_dev_mem_ptr(p
);
233 copied
= copy_from_user(ptr
, buf
, sz
);
234 unxlate_dev_mem_ptr(p
, ptr
);
236 written
+= sz
- copied
;
253 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
254 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
259 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
262 * Architectures vary in how they handle caching for addresses
263 * outside of main memory.
266 #ifdef pgprot_noncached
267 static int uncached_access(struct file
*file
, phys_addr_t addr
)
269 #if defined(CONFIG_IA64)
271 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
274 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
275 #elif defined(CONFIG_MIPS)
277 extern int __uncached_access(struct file
*file
,
280 return __uncached_access(file
, addr
);
284 * Accessing memory above the top the kernel knows about or through a
286 * that was marked O_DSYNC will be done non-cached.
288 if (file
->f_flags
& O_DSYNC
)
290 return addr
>= __pa(high_memory
);
295 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
296 unsigned long size
, pgprot_t vma_prot
)
298 #ifdef pgprot_noncached
299 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
301 if (uncached_access(file
, offset
))
302 return pgprot_noncached(vma_prot
);
309 static unsigned long get_unmapped_area_mem(struct file
*file
,
315 if (!valid_mmap_phys_addr_range(pgoff
, len
))
316 return (unsigned long) -EINVAL
;
317 return pgoff
<< PAGE_SHIFT
;
320 /* permit direct mmap, for read, write or exec */
321 static unsigned memory_mmap_capabilities(struct file
*file
)
323 return NOMMU_MAP_DIRECT
|
324 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
327 static unsigned zero_mmap_capabilities(struct file
*file
)
329 return NOMMU_MAP_COPY
;
332 /* can't do an in-place private mapping if there's no MMU */
333 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
335 return vma
->vm_flags
& VM_MAYSHARE
;
339 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
345 static const struct vm_operations_struct mmap_mem_ops
= {
346 #ifdef CONFIG_HAVE_IOREMAP_PROT
347 .access
= generic_access_phys
351 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
353 size_t size
= vma
->vm_end
- vma
->vm_start
;
354 phys_addr_t offset
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
356 /* It's illegal to wrap around the end of the physical address space. */
357 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
360 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
363 if (!private_mapping_ok(vma
))
366 if (!range_is_allowed(vma
->vm_pgoff
, size
))
369 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
373 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
377 vma
->vm_ops
= &mmap_mem_ops
;
379 /* Remap-pfn-range will mark the range VM_IO */
380 if (remap_pfn_range(vma
,
384 vma
->vm_page_prot
)) {
390 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
394 /* Turn a kernel-virtual address into a physical page frame */
395 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
398 * RED-PEN: on some architectures there is more mapped memory than
399 * available in mem_map which pfn_valid checks for. Perhaps should add a
402 * RED-PEN: vmalloc is not supported right now.
408 return mmap_mem(file
, vma
);
412 * This function reads the *virtual* memory as seen by the kernel.
414 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
415 size_t count
, loff_t
*ppos
)
417 unsigned long p
= *ppos
;
418 ssize_t low_count
, read
, sz
;
419 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
423 if (p
< (unsigned long) high_memory
) {
425 if (count
> (unsigned long)high_memory
- p
)
426 low_count
= (unsigned long)high_memory
- p
;
428 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
429 /* we don't have page 0 mapped on sparc and m68k.. */
430 if (p
< PAGE_SIZE
&& low_count
> 0) {
431 sz
= size_inside_page(p
, low_count
);
432 if (clear_user(buf
, sz
))
441 while (low_count
> 0) {
442 sz
= size_inside_page(p
, low_count
);
445 * On ia64 if a page has been mapped somewhere as
446 * uncached, then it must also be accessed uncached
447 * by the kernel or data corruption may occur
449 kbuf
= xlate_dev_kmem_ptr((void *)p
);
451 if (copy_to_user(buf
, kbuf
, sz
))
462 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
466 sz
= size_inside_page(p
, count
);
467 if (!is_vmalloc_or_module_addr((void *)p
)) {
471 sz
= vread(kbuf
, (char *)p
, sz
);
474 if (copy_to_user(buf
, kbuf
, sz
)) {
483 free_page((unsigned long)kbuf
);
486 return read
? read
: err
;
490 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
491 size_t count
, loff_t
*ppos
)
494 unsigned long copied
;
497 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
498 /* we don't have page 0 mapped on sparc and m68k.. */
500 sz
= size_inside_page(p
, count
);
501 /* Hmm. Do something? */
512 sz
= size_inside_page(p
, count
);
515 * On ia64 if a page has been mapped somewhere as uncached, then
516 * it must also be accessed uncached by the kernel or data
517 * corruption may occur.
519 ptr
= xlate_dev_kmem_ptr((void *)p
);
521 copied
= copy_from_user(ptr
, buf
, sz
);
523 written
+= sz
- copied
;
539 * This function writes to the *virtual* memory as seen by the kernel.
541 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
542 size_t count
, loff_t
*ppos
)
544 unsigned long p
= *ppos
;
547 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
550 if (p
< (unsigned long) high_memory
) {
551 unsigned long to_write
= min_t(unsigned long, count
,
552 (unsigned long)high_memory
- p
);
553 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
554 if (wrote
!= to_write
)
562 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
564 return wrote
? wrote
: -ENOMEM
;
566 unsigned long sz
= size_inside_page(p
, count
);
569 if (!is_vmalloc_or_module_addr((void *)p
)) {
573 n
= copy_from_user(kbuf
, buf
, sz
);
578 vwrite(kbuf
, (char *)p
, sz
);
584 free_page((unsigned long)kbuf
);
588 return virtr
+ wrote
? : err
;
591 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
592 size_t count
, loff_t
*ppos
)
594 unsigned long i
= *ppos
;
595 char __user
*tmp
= buf
;
597 if (!access_ok(VERIFY_WRITE
, buf
, count
))
599 while (count
-- > 0 && i
< 65536) {
600 if (__put_user(inb(i
), tmp
) < 0)
609 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
610 size_t count
, loff_t
*ppos
)
612 unsigned long i
= *ppos
;
613 const char __user
*tmp
= buf
;
615 if (!access_ok(VERIFY_READ
, buf
, count
))
617 while (count
-- > 0 && i
< 65536) {
620 if (__get_user(c
, tmp
)) {
633 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
634 size_t count
, loff_t
*ppos
)
639 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
640 size_t count
, loff_t
*ppos
)
645 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
650 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
652 size_t count
= iov_iter_count(from
);
653 iov_iter_advance(from
, count
);
657 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
658 struct splice_desc
*sd
)
663 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
664 loff_t
*ppos
, size_t len
, unsigned int flags
)
666 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
669 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
673 while (iov_iter_count(iter
)) {
674 size_t chunk
= iov_iter_count(iter
), n
;
676 if (chunk
> PAGE_SIZE
)
677 chunk
= PAGE_SIZE
; /* Just for latency reasons */
678 n
= iov_iter_zero(chunk
, iter
);
679 if (!n
&& iov_iter_count(iter
))
680 return written
? written
: -EFAULT
;
682 if (signal_pending(current
))
683 return written
? written
: -ERESTARTSYS
;
689 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
694 if (vma
->vm_flags
& VM_SHARED
)
695 return shmem_zero_setup(vma
);
699 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
700 size_t count
, loff_t
*ppos
)
706 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
707 * can fopen() both devices with "a" now. This was previously impossible.
710 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
712 return file
->f_pos
= 0;
716 * The memory devices use the full 32/64 bits of the offset, and so we cannot
717 * check against negative addresses: they are ok. The return value is weird,
718 * though, in that case (0).
720 * also note that seeking relative to the "end of file" isn't supported:
721 * it has no meaning, so it returns -EINVAL.
723 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
727 mutex_lock(&file_inode(file
)->i_mutex
);
730 offset
+= file
->f_pos
;
732 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
733 if (IS_ERR_VALUE((unsigned long long)offset
)) {
737 file
->f_pos
= offset
;
739 force_successful_syscall_return();
744 mutex_unlock(&file_inode(file
)->i_mutex
);
748 static int open_port(struct inode
*inode
, struct file
*filp
)
750 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
753 #define zero_lseek null_lseek
754 #define full_lseek null_lseek
755 #define write_zero write_null
756 #define write_iter_zero write_iter_null
757 #define open_mem open_port
758 #define open_kmem open_mem
760 static const struct file_operations __maybe_unused mem_fops
= {
761 .llseek
= memory_lseek
,
767 .get_unmapped_area
= get_unmapped_area_mem
,
768 .mmap_capabilities
= memory_mmap_capabilities
,
772 static const struct file_operations __maybe_unused kmem_fops
= {
773 .llseek
= memory_lseek
,
779 .get_unmapped_area
= get_unmapped_area_mem
,
780 .mmap_capabilities
= memory_mmap_capabilities
,
784 static const struct file_operations null_fops
= {
785 .llseek
= null_lseek
,
788 .read_iter
= read_iter_null
,
789 .write_iter
= write_iter_null
,
790 .splice_write
= splice_write_null
,
793 static const struct file_operations __maybe_unused port_fops
= {
794 .llseek
= memory_lseek
,
800 static const struct file_operations zero_fops
= {
801 .llseek
= zero_lseek
,
803 .read_iter
= read_iter_zero
,
804 .write_iter
= write_iter_zero
,
807 .mmap_capabilities
= zero_mmap_capabilities
,
811 static const struct file_operations full_fops
= {
812 .llseek
= full_lseek
,
813 .read_iter
= read_iter_zero
,
817 static const struct memdev
{
820 const struct file_operations
*fops
;
824 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
826 #ifdef CONFIG_DEVKMEM
827 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
829 [3] = { "null", 0666, &null_fops
, 0 },
830 #ifdef CONFIG_DEVPORT
831 [4] = { "port", 0, &port_fops
, 0 },
833 [5] = { "zero", 0666, &zero_fops
, 0 },
834 [7] = { "full", 0666, &full_fops
, 0 },
835 [8] = { "random", 0666, &random_fops
, 0 },
836 [9] = { "urandom", 0666, &urandom_fops
, 0 },
838 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
840 #ifdef CONFIG_KNOX_KAP
841 [13] = { "knox_kap", 0664, &knox_kap_fops
, 0 },
843 #ifdef CONFIG_MST_LDO
844 [14] = { "mst_ctrl", 0666, &mst_ctrl_fops
, 0 },
848 static int memory_open(struct inode
*inode
, struct file
*filp
)
851 const struct memdev
*dev
;
853 minor
= iminor(inode
);
854 if (minor
>= ARRAY_SIZE(devlist
))
857 dev
= &devlist
[minor
];
861 filp
->f_op
= dev
->fops
;
862 filp
->f_mode
|= dev
->fmode
;
865 return dev
->fops
->open(inode
, filp
);
870 static const struct file_operations memory_fops
= {
872 .llseek
= noop_llseek
,
875 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
877 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
878 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
882 static struct class *mem_class
;
884 static int __init
chr_dev_init(void)
888 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
889 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
891 mem_class
= class_create(THIS_MODULE
, "mem");
892 if (IS_ERR(mem_class
))
893 return PTR_ERR(mem_class
);
895 mem_class
->devnode
= mem_devnode
;
896 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
897 if (!devlist
[minor
].name
)
903 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
906 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
907 NULL
, devlist
[minor
].name
);
913 fs_initcall(chr_dev_init
);