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/bootmem.h>
26 #include <linux/splice.h>
27 #include <linux/pfn.h>
28 #include <linux/export.h>
30 #include <linux/aio.h>
32 #include <asm/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 range_is_allowed(unsigned long pfn
, unsigned long size
)
65 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
70 if (!devmem_is_allowed(pfn
)) {
72 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
73 current
->comm
, from
, to
);
82 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
88 void __weak
unxlate_dev_mem_ptr(unsigned long 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 /* can't do an in-place private mapping if there's no MMU */
289 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
291 return vma
->vm_flags
& VM_MAYSHARE
;
294 #define get_unmapped_area_mem NULL
296 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
302 static const struct vm_operations_struct mmap_mem_ops
= {
303 #ifdef CONFIG_HAVE_IOREMAP_PROT
304 .access
= generic_access_phys
308 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
310 size_t size
= vma
->vm_end
- vma
->vm_start
;
312 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
315 if (!private_mapping_ok(vma
))
318 if (!range_is_allowed(vma
->vm_pgoff
, size
))
321 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
325 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
329 vma
->vm_ops
= &mmap_mem_ops
;
331 /* Remap-pfn-range will mark the range VM_IO */
332 if (remap_pfn_range(vma
,
336 vma
->vm_page_prot
)) {
342 #ifdef CONFIG_DEVKMEM
343 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
347 /* Turn a kernel-virtual address into a physical page frame */
348 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
351 * RED-PEN: on some architectures there is more mapped memory than
352 * available in mem_map which pfn_valid checks for. Perhaps should add a
355 * RED-PEN: vmalloc is not supported right now.
361 return mmap_mem(file
, vma
);
365 #ifdef CONFIG_DEVKMEM
367 * This function reads the *virtual* memory as seen by the kernel.
369 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
370 size_t count
, loff_t
*ppos
)
372 unsigned long p
= *ppos
;
373 ssize_t low_count
, read
, sz
;
374 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
378 if (p
< (unsigned long) high_memory
) {
380 if (count
> (unsigned long)high_memory
- p
)
381 low_count
= (unsigned long)high_memory
- p
;
383 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
384 /* we don't have page 0 mapped on sparc and m68k.. */
385 if (p
< PAGE_SIZE
&& low_count
> 0) {
386 sz
= size_inside_page(p
, low_count
);
387 if (clear_user(buf
, sz
))
396 while (low_count
> 0) {
397 sz
= size_inside_page(p
, low_count
);
400 * On ia64 if a page has been mapped somewhere as
401 * uncached, then it must also be accessed uncached
402 * by the kernel or data corruption may occur
404 kbuf
= xlate_dev_kmem_ptr((char *)p
);
406 if (copy_to_user(buf
, kbuf
, sz
))
417 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
421 sz
= size_inside_page(p
, count
);
422 if (!is_vmalloc_or_module_addr((void *)p
)) {
426 sz
= vread(kbuf
, (char *)p
, sz
);
429 if (copy_to_user(buf
, kbuf
, sz
)) {
438 free_page((unsigned long)kbuf
);
441 return read
? read
: err
;
445 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
446 size_t count
, loff_t
*ppos
)
449 unsigned long copied
;
452 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
453 /* we don't have page 0 mapped on sparc and m68k.. */
455 sz
= size_inside_page(p
, count
);
456 /* Hmm. Do something? */
467 sz
= size_inside_page(p
, count
);
470 * On ia64 if a page has been mapped somewhere as uncached, then
471 * it must also be accessed uncached by the kernel or data
472 * corruption may occur.
474 ptr
= xlate_dev_kmem_ptr((char *)p
);
476 copied
= copy_from_user(ptr
, buf
, sz
);
478 written
+= sz
- copied
;
494 * This function writes to the *virtual* memory as seen by the kernel.
496 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
497 size_t count
, loff_t
*ppos
)
499 unsigned long p
= *ppos
;
502 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
505 if (p
< (unsigned long) high_memory
) {
506 unsigned long to_write
= min_t(unsigned long, count
,
507 (unsigned long)high_memory
- p
);
508 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
509 if (wrote
!= to_write
)
517 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
519 return wrote
? wrote
: -ENOMEM
;
521 unsigned long sz
= size_inside_page(p
, count
);
524 if (!is_vmalloc_or_module_addr((void *)p
)) {
528 n
= copy_from_user(kbuf
, buf
, sz
);
533 vwrite(kbuf
, (char *)p
, sz
);
539 free_page((unsigned long)kbuf
);
543 return virtr
+ wrote
? : err
;
547 #ifdef CONFIG_DEVPORT
548 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
549 size_t count
, loff_t
*ppos
)
551 unsigned long i
= *ppos
;
552 char __user
*tmp
= buf
;
554 if (!access_ok(VERIFY_WRITE
, buf
, count
))
556 while (count
-- > 0 && i
< 65536) {
557 if (__put_user(inb(i
), tmp
) < 0)
566 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
567 size_t count
, loff_t
*ppos
)
569 unsigned long i
= *ppos
;
570 const char __user
*tmp
= buf
;
572 if (!access_ok(VERIFY_READ
, buf
, count
))
574 while (count
-- > 0 && i
< 65536) {
576 if (__get_user(c
, tmp
)) {
590 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
591 size_t count
, loff_t
*ppos
)
596 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
597 size_t count
, loff_t
*ppos
)
602 static ssize_t
aio_read_null(struct kiocb
*iocb
, const struct iovec
*iov
,
603 unsigned long nr_segs
, loff_t pos
)
608 static ssize_t
aio_write_null(struct kiocb
*iocb
, const struct iovec
*iov
,
609 unsigned long nr_segs
, loff_t pos
)
611 return iov_length(iov
, nr_segs
);
614 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
615 struct splice_desc
*sd
)
620 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
621 loff_t
*ppos
, size_t len
, unsigned int flags
)
623 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
626 static ssize_t
read_zero(struct file
*file
, char __user
*buf
,
627 size_t count
, loff_t
*ppos
)
634 if (!access_ok(VERIFY_WRITE
, buf
, count
))
639 unsigned long unwritten
;
640 size_t chunk
= count
;
642 if (chunk
> PAGE_SIZE
)
643 chunk
= PAGE_SIZE
; /* Just for latency reasons */
644 unwritten
= __clear_user(buf
, chunk
);
645 written
+= chunk
- unwritten
;
648 if (signal_pending(current
))
649 return written
? written
: -ERESTARTSYS
;
654 return written
? written
: -EFAULT
;
657 static ssize_t
aio_read_zero(struct kiocb
*iocb
, const struct iovec
*iov
,
658 unsigned long nr_segs
, loff_t pos
)
664 for (i
= 0; i
< nr_segs
; i
++) {
665 ret
= read_zero(iocb
->ki_filp
, iov
[i
].iov_base
, iov
[i
].iov_len
,
672 return written
? written
: -EFAULT
;
675 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
680 if (vma
->vm_flags
& VM_SHARED
)
681 return shmem_zero_setup(vma
);
685 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
686 size_t count
, loff_t
*ppos
)
692 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
693 * can fopen() both devices with "a" now. This was previously impossible.
696 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
698 return file
->f_pos
= 0;
702 * The memory devices use the full 32/64 bits of the offset, and so we cannot
703 * check against negative addresses: they are ok. The return value is weird,
704 * though, in that case (0).
706 * also note that seeking relative to the "end of file" isn't supported:
707 * it has no meaning, so it returns -EINVAL.
709 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
713 mutex_lock(&file_inode(file
)->i_mutex
);
716 offset
+= file
->f_pos
;
718 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
719 if (IS_ERR_VALUE((unsigned long long)offset
)) {
723 file
->f_pos
= offset
;
725 force_successful_syscall_return();
730 mutex_unlock(&file_inode(file
)->i_mutex
);
734 static int open_port(struct inode
*inode
, struct file
*filp
)
736 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
739 #define zero_lseek null_lseek
740 #define full_lseek null_lseek
741 #define write_zero write_null
742 #define read_full read_zero
743 #define aio_write_zero aio_write_null
744 #define open_mem open_port
745 #define open_kmem open_mem
747 static const struct file_operations mem_fops
= {
748 .llseek
= memory_lseek
,
753 .get_unmapped_area
= get_unmapped_area_mem
,
756 #ifdef CONFIG_DEVKMEM
757 static const struct file_operations kmem_fops
= {
758 .llseek
= memory_lseek
,
763 .get_unmapped_area
= get_unmapped_area_mem
,
767 static const struct file_operations null_fops
= {
768 .llseek
= null_lseek
,
771 .aio_read
= aio_read_null
,
772 .aio_write
= aio_write_null
,
773 .splice_write
= splice_write_null
,
776 #ifdef CONFIG_DEVPORT
777 static const struct file_operations port_fops
= {
778 .llseek
= memory_lseek
,
785 static const struct file_operations zero_fops
= {
786 .llseek
= zero_lseek
,
789 .aio_read
= aio_read_zero
,
790 .aio_write
= aio_write_zero
,
795 * capabilities for /dev/zero
796 * - permits private mappings, "copies" are taken of the source of zeros
797 * - no writeback happens
799 static struct backing_dev_info zero_bdi
= {
801 .capabilities
= BDI_CAP_MAP_COPY
| BDI_CAP_NO_ACCT_AND_WRITEBACK
,
804 static const struct file_operations full_fops
= {
805 .llseek
= full_lseek
,
810 static const struct memdev
{
813 const struct file_operations
*fops
;
814 struct backing_dev_info
*dev_info
;
816 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
817 #ifdef CONFIG_DEVKMEM
818 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
820 [3] = { "null", 0666, &null_fops
, NULL
},
821 #ifdef CONFIG_DEVPORT
822 [4] = { "port", 0, &port_fops
, NULL
},
824 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
825 [7] = { "full", 0666, &full_fops
, NULL
},
826 [8] = { "random", 0666, &random_fops
, NULL
},
827 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
829 [11] = { "kmsg", 0644, &kmsg_fops
, NULL
},
833 static int memory_open(struct inode
*inode
, struct file
*filp
)
836 const struct memdev
*dev
;
838 minor
= iminor(inode
);
839 if (minor
>= ARRAY_SIZE(devlist
))
842 dev
= &devlist
[minor
];
846 filp
->f_op
= dev
->fops
;
848 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
850 /* Is /dev/mem or /dev/kmem ? */
851 if (dev
->dev_info
== &directly_mappable_cdev_bdi
)
852 filp
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
855 return dev
->fops
->open(inode
, filp
);
860 static const struct file_operations memory_fops
= {
862 .llseek
= noop_llseek
,
865 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
867 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
868 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
872 static struct class *mem_class
;
874 static int __init
chr_dev_init(void)
879 err
= bdi_init(&zero_bdi
);
883 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
884 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
886 mem_class
= class_create(THIS_MODULE
, "mem");
887 if (IS_ERR(mem_class
))
888 return PTR_ERR(mem_class
);
890 mem_class
->devnode
= mem_devnode
;
891 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
892 if (!devlist
[minor
].name
)
898 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
901 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
902 NULL
, devlist
[minor
].name
);
908 fs_initcall(chr_dev_init
);