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/aio.h>
31 #include <asm/uaccess.h>
34 # include <linux/efi.h>
37 #define DEVPORT_MINOR 4
39 static inline unsigned long size_inside_page(unsigned long start
,
44 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
49 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
50 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
52 return addr
+ count
<= __pa(high_memory
);
55 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
61 #ifdef CONFIG_STRICT_DEVMEM
62 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
64 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
69 if (!devmem_is_allowed(pfn
)) {
71 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
72 current
->comm
, from
, to
);
81 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
87 #ifndef unxlate_dev_mem_ptr
88 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
89 void __weak
unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
95 * This funcion reads the *physical* memory. The f_pos points directly to the
98 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
99 size_t count
, loff_t
*ppos
)
101 phys_addr_t p
= *ppos
;
108 if (!valid_phys_addr_range(p
, count
))
111 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
112 /* we don't have page 0 mapped on sparc and m68k.. */
114 sz
= size_inside_page(p
, count
);
116 if (clear_user(buf
, sz
))
127 unsigned long remaining
;
129 sz
= size_inside_page(p
, count
);
131 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
))
135 * On ia64 if a page has been mapped somewhere as uncached, then
136 * it must also be accessed uncached by the kernel or data
137 * corruption may occur.
139 ptr
= xlate_dev_mem_ptr(p
);
143 remaining
= copy_to_user(buf
, ptr
, sz
);
144 unxlate_dev_mem_ptr(p
, ptr
);
158 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
159 size_t count
, loff_t
*ppos
)
161 phys_addr_t p
= *ppos
;
163 unsigned long copied
;
169 if (!valid_phys_addr_range(p
, count
))
174 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
175 /* we don't have page 0 mapped on sparc and m68k.. */
177 sz
= size_inside_page(p
, count
);
178 /* Hmm. Do something? */
187 sz
= size_inside_page(p
, count
);
189 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
193 * On ia64 if a page has been mapped somewhere as uncached, then
194 * it must also be accessed uncached by the kernel or data
195 * corruption may occur.
197 ptr
= xlate_dev_mem_ptr(p
);
204 copied
= copy_from_user(ptr
, buf
, sz
);
205 unxlate_dev_mem_ptr(p
, ptr
);
207 written
+= sz
- copied
;
223 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
224 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
229 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
232 * Architectures vary in how they handle caching for addresses
233 * outside of main memory.
236 #ifdef pgprot_noncached
237 static int uncached_access(struct file
*file
, phys_addr_t addr
)
239 #if defined(CONFIG_IA64)
241 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
244 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
245 #elif defined(CONFIG_MIPS)
247 extern int __uncached_access(struct file
*file
,
250 return __uncached_access(file
, addr
);
254 * Accessing memory above the top the kernel knows about or through a
256 * that was marked O_DSYNC will be done non-cached.
258 if (file
->f_flags
& O_DSYNC
)
260 return addr
>= __pa(high_memory
);
265 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
266 unsigned long size
, pgprot_t vma_prot
)
268 #ifdef pgprot_noncached
269 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
271 if (uncached_access(file
, offset
))
272 return pgprot_noncached(vma_prot
);
279 static unsigned long get_unmapped_area_mem(struct file
*file
,
285 if (!valid_mmap_phys_addr_range(pgoff
, len
))
286 return (unsigned long) -EINVAL
;
287 return pgoff
<< PAGE_SHIFT
;
290 /* permit direct mmap, for read, write or exec */
291 static unsigned memory_mmap_capabilities(struct file
*file
)
293 return NOMMU_MAP_DIRECT
|
294 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
| NOMMU_MAP_EXEC
;
297 static unsigned zero_mmap_capabilities(struct file
*file
)
299 return NOMMU_MAP_COPY
;
302 /* can't do an in-place private mapping if there's no MMU */
303 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
305 return vma
->vm_flags
& VM_MAYSHARE
;
309 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
315 static const struct vm_operations_struct mmap_mem_ops
= {
316 #ifdef CONFIG_HAVE_IOREMAP_PROT
317 .access
= generic_access_phys
321 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
323 size_t size
= vma
->vm_end
- vma
->vm_start
;
325 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
328 if (!private_mapping_ok(vma
))
331 if (!range_is_allowed(vma
->vm_pgoff
, size
))
334 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
338 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
342 vma
->vm_ops
= &mmap_mem_ops
;
344 /* Remap-pfn-range will mark the range VM_IO */
345 if (remap_pfn_range(vma
,
349 vma
->vm_page_prot
)) {
355 #ifdef CONFIG_DEVKMEM
356 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
360 /* Turn a kernel-virtual address into a physical page frame */
361 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
364 * RED-PEN: on some architectures there is more mapped memory than
365 * available in mem_map which pfn_valid checks for. Perhaps should add a
368 * RED-PEN: vmalloc is not supported right now.
374 return mmap_mem(file
, vma
);
378 #ifdef CONFIG_DEVKMEM
380 * This function reads the *virtual* memory as seen by the kernel.
382 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
383 size_t count
, loff_t
*ppos
)
385 unsigned long p
= *ppos
;
386 ssize_t low_count
, read
, sz
;
387 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
391 if (p
< (unsigned long) high_memory
) {
393 if (count
> (unsigned long)high_memory
- p
)
394 low_count
= (unsigned long)high_memory
- p
;
396 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
397 /* we don't have page 0 mapped on sparc and m68k.. */
398 if (p
< PAGE_SIZE
&& low_count
> 0) {
399 sz
= size_inside_page(p
, low_count
);
400 if (clear_user(buf
, sz
))
409 while (low_count
> 0) {
410 sz
= size_inside_page(p
, low_count
);
413 * On ia64 if a page has been mapped somewhere as
414 * uncached, then it must also be accessed uncached
415 * by the kernel or data corruption may occur
417 kbuf
= xlate_dev_kmem_ptr((void *)p
);
419 if (copy_to_user(buf
, kbuf
, sz
))
430 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
434 sz
= size_inside_page(p
, count
);
435 if (!is_vmalloc_or_module_addr((void *)p
)) {
439 sz
= vread(kbuf
, (char *)p
, sz
);
442 if (copy_to_user(buf
, kbuf
, sz
)) {
451 free_page((unsigned long)kbuf
);
454 return read
? read
: err
;
458 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
459 size_t count
, loff_t
*ppos
)
462 unsigned long copied
;
465 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
466 /* we don't have page 0 mapped on sparc and m68k.. */
468 sz
= size_inside_page(p
, count
);
469 /* Hmm. Do something? */
480 sz
= size_inside_page(p
, count
);
483 * On ia64 if a page has been mapped somewhere as uncached, then
484 * it must also be accessed uncached by the kernel or data
485 * corruption may occur.
487 ptr
= xlate_dev_kmem_ptr((void *)p
);
489 copied
= copy_from_user(ptr
, buf
, sz
);
491 written
+= sz
- copied
;
507 * This function writes to the *virtual* memory as seen by the kernel.
509 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
510 size_t count
, loff_t
*ppos
)
512 unsigned long p
= *ppos
;
515 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
518 if (p
< (unsigned long) high_memory
) {
519 unsigned long to_write
= min_t(unsigned long, count
,
520 (unsigned long)high_memory
- p
);
521 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
522 if (wrote
!= to_write
)
530 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
532 return wrote
? wrote
: -ENOMEM
;
534 unsigned long sz
= size_inside_page(p
, count
);
537 if (!is_vmalloc_or_module_addr((void *)p
)) {
541 n
= copy_from_user(kbuf
, buf
, sz
);
546 vwrite(kbuf
, (char *)p
, sz
);
552 free_page((unsigned long)kbuf
);
556 return virtr
+ wrote
? : err
;
560 #ifdef CONFIG_DEVPORT
561 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
562 size_t count
, loff_t
*ppos
)
564 unsigned long i
= *ppos
;
565 char __user
*tmp
= buf
;
567 if (!access_ok(VERIFY_WRITE
, buf
, count
))
569 while (count
-- > 0 && i
< 65536) {
570 if (__put_user(inb(i
), tmp
) < 0)
579 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
580 size_t count
, loff_t
*ppos
)
582 unsigned long i
= *ppos
;
583 const char __user
*tmp
= buf
;
585 if (!access_ok(VERIFY_READ
, buf
, count
))
587 while (count
-- > 0 && i
< 65536) {
589 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
aio_read_null(struct kiocb
*iocb
, const struct iovec
*iov
,
616 unsigned long nr_segs
, loff_t pos
)
621 static ssize_t
aio_write_null(struct kiocb
*iocb
, const struct iovec
*iov
,
622 unsigned long nr_segs
, loff_t pos
)
624 return iov_length(iov
, nr_segs
);
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
;
645 if (chunk
> PAGE_SIZE
)
646 chunk
= PAGE_SIZE
; /* Just for latency reasons */
647 n
= iov_iter_zero(chunk
, iter
);
648 if (!n
&& iov_iter_count(iter
))
649 return written
? written
: -EFAULT
;
651 if (signal_pending(current
))
652 return written
? written
: -ERESTARTSYS
;
658 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
663 if (vma
->vm_flags
& VM_SHARED
)
664 return shmem_zero_setup(vma
);
668 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
669 size_t count
, loff_t
*ppos
)
675 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
676 * can fopen() both devices with "a" now. This was previously impossible.
679 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
681 return file
->f_pos
= 0;
685 * The memory devices use the full 32/64 bits of the offset, and so we cannot
686 * check against negative addresses: they are ok. The return value is weird,
687 * though, in that case (0).
689 * also note that seeking relative to the "end of file" isn't supported:
690 * it has no meaning, so it returns -EINVAL.
692 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
696 mutex_lock(&file_inode(file
)->i_mutex
);
699 offset
+= file
->f_pos
;
701 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
702 if (IS_ERR_VALUE((unsigned long long)offset
)) {
706 file
->f_pos
= offset
;
708 force_successful_syscall_return();
713 mutex_unlock(&file_inode(file
)->i_mutex
);
717 static int open_port(struct inode
*inode
, struct file
*filp
)
719 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
722 #define zero_lseek null_lseek
723 #define full_lseek null_lseek
724 #define write_zero write_null
725 #define aio_write_zero aio_write_null
726 #define open_mem open_port
727 #define open_kmem open_mem
729 static const struct file_operations mem_fops
= {
730 .llseek
= memory_lseek
,
736 .get_unmapped_area
= get_unmapped_area_mem
,
737 .mmap_capabilities
= memory_mmap_capabilities
,
741 #ifdef CONFIG_DEVKMEM
742 static const struct file_operations kmem_fops
= {
743 .llseek
= memory_lseek
,
749 .get_unmapped_area
= get_unmapped_area_mem
,
750 .mmap_capabilities
= memory_mmap_capabilities
,
755 static const struct file_operations null_fops
= {
756 .llseek
= null_lseek
,
759 .aio_read
= aio_read_null
,
760 .aio_write
= aio_write_null
,
761 .splice_write
= splice_write_null
,
764 #ifdef CONFIG_DEVPORT
765 static const struct file_operations port_fops
= {
766 .llseek
= memory_lseek
,
773 static const struct file_operations zero_fops
= {
774 .llseek
= zero_lseek
,
775 .read
= new_sync_read
,
777 .read_iter
= read_iter_zero
,
778 .aio_write
= aio_write_zero
,
781 .mmap_capabilities
= zero_mmap_capabilities
,
785 static const struct file_operations full_fops
= {
786 .llseek
= full_lseek
,
787 .read
= new_sync_read
,
788 .read_iter
= read_iter_zero
,
792 static const struct memdev
{
795 const struct file_operations
*fops
;
798 [1] = { "mem", 0, &mem_fops
, FMODE_UNSIGNED_OFFSET
},
799 #ifdef CONFIG_DEVKMEM
800 [2] = { "kmem", 0, &kmem_fops
, FMODE_UNSIGNED_OFFSET
},
802 [3] = { "null", 0666, &null_fops
, 0 },
803 #ifdef CONFIG_DEVPORT
804 [4] = { "port", 0, &port_fops
, 0 },
806 [5] = { "zero", 0666, &zero_fops
, 0 },
807 [7] = { "full", 0666, &full_fops
, 0 },
808 [8] = { "random", 0666, &random_fops
, 0 },
809 [9] = { "urandom", 0666, &urandom_fops
, 0 },
811 [11] = { "kmsg", 0644, &kmsg_fops
, 0 },
815 static int memory_open(struct inode
*inode
, struct file
*filp
)
818 const struct memdev
*dev
;
820 minor
= iminor(inode
);
821 if (minor
>= ARRAY_SIZE(devlist
))
824 dev
= &devlist
[minor
];
828 filp
->f_op
= dev
->fops
;
829 filp
->f_mode
|= dev
->fmode
;
832 return dev
->fops
->open(inode
, filp
);
837 static const struct file_operations memory_fops
= {
839 .llseek
= noop_llseek
,
842 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
844 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
845 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
849 static struct class *mem_class
;
851 static int __init
chr_dev_init(void)
855 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
856 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
858 mem_class
= class_create(THIS_MODULE
, "mem");
859 if (IS_ERR(mem_class
))
860 return PTR_ERR(mem_class
);
862 mem_class
->devnode
= mem_devnode
;
863 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
864 if (!devlist
[minor
].name
)
870 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
873 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
874 NULL
, devlist
[minor
].name
);
880 fs_initcall(chr_dev_init
);