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/security.h>
22 #include <linux/ptrace.h>
23 #include <linux/device.h>
24 #include <linux/highmem.h>
25 #include <linux/backing-dev.h>
26 #include <linux/shmem_fs.h>
27 #include <linux/splice.h>
28 #include <linux/pfn.h>
29 #include <linux/export.h>
31 #include <linux/uio.h>
33 #include <linux/uaccess.h>
36 # include <linux/efi.h>
39 #define DEVPORT_MINOR 4
41 #if defined(CONFIG_GRKERNSEC) && !defined(CONFIG_GRKERNSEC_NO_RBAC)
42 extern const struct file_operations grsec_fops
;
45 static inline unsigned long size_inside_page(unsigned long start
,
50 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
55 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
56 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
58 return addr
+ count
<= __pa(high_memory
);
61 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
67 #ifdef CONFIG_STRICT_DEVMEM
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
)) {
76 #ifdef CONFIG_GRKERNSEC_KMEM
77 gr_handle_mem_readwrite(from
, to
);
86 #elif defined(CONFIG_GRKERNSEC_KMEM)
87 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
92 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
98 #ifndef unxlate_dev_mem_ptr
99 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
100 void __weak
unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
106 * This funcion reads the *physical* memory. The f_pos points directly to the
109 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
110 size_t count
, loff_t
*ppos
)
112 phys_addr_t p
= *ppos
;
120 if (!valid_phys_addr_range(p
, count
))
123 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
124 /* we don't have page 0 mapped on sparc and m68k.. */
126 sz
= size_inside_page(p
, count
);
128 if (clear_user(buf
, sz
))
138 temp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
|GFP_USERCOPY
);
143 unsigned long remaining
;
145 sz
= size_inside_page(p
, count
);
147 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
)) {
153 * On ia64 if a page has been mapped somewhere as uncached, then
154 * it must also be accessed uncached by the kernel or data
155 * corruption may occur.
157 ptr
= xlate_dev_mem_ptr(p
);
158 if (!ptr
|| probe_kernel_read(temp
, ptr
, sz
)) {
163 remaining
= copy_to_user(buf
, temp
, sz
);
164 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? */
211 sz
= size_inside_page(p
, count
);
213 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
217 * On ia64 if a page has been mapped somewhere as uncached, then
218 * it must also be accessed uncached by the kernel or data
219 * corruption may occur.
221 ptr
= xlate_dev_mem_ptr(p
);
228 copied
= copy_from_user(ptr
, buf
, sz
);
229 unxlate_dev_mem_ptr(p
, ptr
);
231 written
+= sz
- copied
;
247 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
248 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
253 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
256 * Architectures vary in how they handle caching for addresses
257 * outside of main memory.
260 #ifdef pgprot_noncached
261 static int uncached_access(struct file
*file
, phys_addr_t addr
)
263 #if defined(CONFIG_IA64)
265 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
268 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
269 #elif defined(CONFIG_MIPS)
271 extern int __uncached_access(struct file
*file
,
274 return __uncached_access(file
, addr
);
278 * Accessing memory above the top the kernel knows about or through a
280 * that was marked O_DSYNC will be done non-cached.
282 if (file
->f_flags
& O_DSYNC
)
284 return addr
>= __pa(high_memory
);
289 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
290 unsigned long size
, pgprot_t vma_prot
)
292 #ifdef pgprot_noncached
293 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
295 if (uncached_access(file
, offset
))
296 return pgprot_noncached(vma_prot
);
303 static unsigned long get_unmapped_area_mem(struct file
*file
,
309 if (!valid_mmap_phys_addr_range(pgoff
, len
))
310 return (unsigned long) -EINVAL
;
311 return pgoff
<< PAGE_SHIFT
;
314 /* permit direct mmap, for read, write or exec */
315 static unsigned memory_mmap_capabilities(struct file
*file
)
317 return NOMMU_MAP_DIRECT
|
318 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
321 static unsigned zero_mmap_capabilities(struct file
*file
)
323 return NOMMU_MAP_COPY
;
326 /* can't do an in-place private mapping if there's no MMU */
327 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
329 return vma
->vm_flags
& VM_MAYSHARE
;
333 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
339 static const struct vm_operations_struct mmap_mem_ops
= {
340 #ifdef CONFIG_HAVE_IOREMAP_PROT
341 .access
= generic_access_phys
345 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
347 size_t size
= vma
->vm_end
- vma
->vm_start
;
348 phys_addr_t offset
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
350 /* It's illegal to wrap around the end of the physical address space. */
351 if (offset
+ (phys_addr_t
)size
- 1 < offset
)
354 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
357 if (!private_mapping_ok(vma
))
360 if (!range_is_allowed(vma
->vm_pgoff
, size
))
363 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
367 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
371 vma
->vm_ops
= &mmap_mem_ops
;
373 /* Remap-pfn-range will mark the range VM_IO */
374 if (remap_pfn_range(vma
,
378 vma
->vm_page_prot
)) {
384 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
388 /* Turn a kernel-virtual address into a physical page frame */
389 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
392 * RED-PEN: on some architectures there is more mapped memory than
393 * available in mem_map which pfn_valid checks for. Perhaps should add a
396 * RED-PEN: vmalloc is not supported right now.
402 return mmap_mem(file
, vma
);
406 * This function reads the *virtual* memory as seen by the kernel.
408 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
409 size_t count
, loff_t
*ppos
)
411 unsigned long p
= *ppos
;
412 ssize_t low_count
, read
, sz
;
413 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
417 if (p
< (unsigned long) high_memory
) {
421 if (count
> (unsigned long)high_memory
- p
)
422 low_count
= (unsigned long)high_memory
- p
;
424 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
425 /* we don't have page 0 mapped on sparc and m68k.. */
426 if (p
< PAGE_SIZE
&& low_count
> 0) {
427 sz
= size_inside_page(p
, low_count
);
428 if (clear_user(buf
, sz
))
438 temp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
|GFP_USERCOPY
);
442 while (low_count
> 0) {
443 sz
= size_inside_page(p
, low_count
);
446 * On ia64 if a page has been mapped somewhere as
447 * uncached, then it must also be accessed uncached
448 * by the kernel or data corruption may occur
450 kbuf
= xlate_dev_kmem_ptr((void *)p
);
451 if (!virt_addr_valid(kbuf
))
454 if (probe_kernel_read(temp
, kbuf
, sz
) || copy_to_user(buf
, temp
, sz
)) {
469 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
473 sz
= size_inside_page(p
, count
);
474 if (!is_vmalloc_or_module_addr((void *)p
)) {
478 sz
= vread(kbuf
, (char *)p
, sz
);
481 if (copy_to_user(buf
, kbuf
, sz
)) {
490 free_page((unsigned long)kbuf
);
493 return read
? read
: err
;
497 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
498 size_t count
, loff_t
*ppos
)
501 unsigned long copied
;
504 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
505 /* we don't have page 0 mapped on sparc and m68k.. */
507 sz
= size_inside_page(p
, count
);
508 /* Hmm. Do something? */
519 sz
= size_inside_page(p
, count
);
522 * On ia64 if a page has been mapped somewhere as uncached, then
523 * it must also be accessed uncached by the kernel or data
524 * corruption may occur.
526 ptr
= xlate_dev_kmem_ptr((void *)p
);
527 if (!virt_addr_valid(ptr
))
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
;
600 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
601 size_t count
, loff_t
*ppos
)
603 unsigned long i
= *ppos
;
604 char __user
*tmp
= buf
;
606 if (!access_ok(VERIFY_WRITE
, buf
, count
))
608 while (count
-- > 0 && i
< 65536) {
609 if (__put_user(inb(i
), tmp
) < 0)
618 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
619 size_t count
, loff_t
*ppos
)
621 unsigned long i
= *ppos
;
622 const char __user
*tmp
= buf
;
624 if (!access_ok(VERIFY_READ
, buf
, count
))
626 while (count
-- > 0 && i
< 65536) {
629 if (__get_user(c
, tmp
)) {
642 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
643 size_t count
, loff_t
*ppos
)
648 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
649 size_t count
, loff_t
*ppos
)
654 static ssize_t
read_iter_null(struct kiocb
*iocb
, struct iov_iter
*to
)
659 static ssize_t
write_iter_null(struct kiocb
*iocb
, struct iov_iter
*from
)
661 size_t count
= iov_iter_count(from
);
662 iov_iter_advance(from
, count
);
666 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
667 struct splice_desc
*sd
)
672 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
673 loff_t
*ppos
, size_t len
, unsigned int flags
)
675 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
678 static ssize_t
read_iter_zero(struct kiocb
*iocb
, struct iov_iter
*iter
)
682 while (iov_iter_count(iter
)) {
683 size_t chunk
= iov_iter_count(iter
), n
;
685 if (chunk
> PAGE_SIZE
)
686 chunk
= PAGE_SIZE
; /* Just for latency reasons */
687 n
= iov_iter_zero(chunk
, iter
);
688 if (!n
&& iov_iter_count(iter
))
689 return written
? written
: -EFAULT
;
691 if (signal_pending(current
))
692 return written
? written
: -ERESTARTSYS
;
698 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
703 if (vma
->vm_flags
& VM_SHARED
)
704 return shmem_zero_setup(vma
);
708 static unsigned long get_unmapped_area_zero(struct file
*file
,
709 unsigned long addr
, unsigned long len
,
710 unsigned long pgoff
, unsigned long flags
)
713 if (flags
& MAP_SHARED
) {
715 * mmap_zero() will call shmem_zero_setup() to create a file,
716 * so use shmem's get_unmapped_area in case it can be huge;
717 * and pass NULL for file as in mmap.c's get_unmapped_area(),
718 * so as not to confuse shmem with our handle on "/dev/zero".
720 return shmem_get_unmapped_area(NULL
, addr
, len
, pgoff
, flags
);
723 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
724 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
730 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
731 size_t count
, loff_t
*ppos
)
737 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
738 * can fopen() both devices with "a" now. This was previously impossible.
741 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
743 return file
->f_pos
= 0;
747 * The memory devices use the full 32/64 bits of the offset, and so we cannot
748 * check against negative addresses: they are ok. The return value is weird,
749 * though, in that case (0).
751 * also note that seeking relative to the "end of file" isn't supported:
752 * it has no meaning, so it returns -EINVAL.
754 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
758 inode_lock(file_inode(file
));
761 offset
+= file
->f_pos
;
763 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
764 if ((unsigned long long)offset
>= -MAX_ERRNO
) {
768 file
->f_pos
= offset
;
770 force_successful_syscall_return();
775 inode_unlock(file_inode(file
));
779 static int open_port(struct inode
*inode
, struct file
*filp
)
781 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
784 #define zero_lseek null_lseek
785 #define full_lseek null_lseek
786 #define write_zero write_null
787 #define write_iter_zero write_iter_null
788 #define open_mem open_port
789 #define open_kmem open_mem
791 static const struct file_operations __maybe_unused mem_fops
= {
792 .llseek
= memory_lseek
,
798 .get_unmapped_area
= get_unmapped_area_mem
,
799 .mmap_capabilities
= memory_mmap_capabilities
,
803 static const struct file_operations __maybe_unused kmem_fops
= {
804 .llseek
= memory_lseek
,
810 .get_unmapped_area
= get_unmapped_area_mem
,
811 .mmap_capabilities
= memory_mmap_capabilities
,
815 static const struct file_operations null_fops
= {
816 .llseek
= null_lseek
,
819 .read_iter
= read_iter_null
,
820 .write_iter
= write_iter_null
,
821 .splice_write
= splice_write_null
,
824 static const struct file_operations __maybe_unused port_fops
= {
825 .llseek
= memory_lseek
,
831 static const struct file_operations zero_fops
= {
832 .llseek
= zero_lseek
,
834 .read_iter
= read_iter_zero
,
835 .write_iter
= write_iter_zero
,
837 .get_unmapped_area
= get_unmapped_area_zero
,
839 .mmap_capabilities
= zero_mmap_capabilities
,
843 static const struct file_operations full_fops
= {
844 .llseek
= full_lseek
,
845 .read_iter
= read_iter_zero
,
849 static const struct memdev
{
852 const struct file_operations
*fops
;
856 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
858 #ifdef CONFIG_DEVKMEM
859 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
861 [3] = { "null", 0666, &null_fops
, 0 },
862 #ifdef CONFIG_DEVPORT
863 [4] = { "port", 0, &port_fops
, 0 },
865 [5] = { "zero", 0666, &zero_fops
, 0 },
866 [7] = { "full", 0666, &full_fops
, 0 },
867 [8] = { "random", 0666, &random_fops
, 0 },
868 [9] = { "urandom", 0666, &urandom_fops
, 0 },
870 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
872 #if defined(CONFIG_GRKERNSEC) && !defined(CONFIG_GRKERNSEC_NO_RBAC)
873 [13] = { "grsec",S_IRUSR
| S_IWUGO
, &grsec_fops
, 0 },
877 static int memory_open(struct inode
*inode
, struct file
*filp
)
880 const struct memdev
*dev
;
882 minor
= iminor(inode
);
883 if (minor
>= ARRAY_SIZE(devlist
))
886 dev
= &devlist
[minor
];
890 filp
->f_op
= dev
->fops
;
891 filp
->f_mode
|= dev
->fmode
;
894 return dev
->fops
->open(inode
, filp
);
899 static const struct file_operations memory_fops
= {
901 .llseek
= noop_llseek
,
904 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
906 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
907 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
911 static struct class *mem_class
;
913 static int __init
chr_dev_init(void)
917 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
918 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
920 mem_class
= class_create(THIS_MODULE
, "mem");
921 if (IS_ERR(mem_class
))
922 return PTR_ERR(mem_class
);
924 mem_class
->devnode
= mem_devnode
;
925 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
926 if (!devlist
[minor
].name
)
932 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
935 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
936 NULL
, "%s", devlist
[minor
].name
);
942 fs_initcall(chr_dev_init
);