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 mmaping 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/crash_dump.h>
25 #include <linux/backing-dev.h>
26 #include <linux/bootmem.h>
27 #include <linux/splice.h>
28 #include <linux/pfn.h>
29 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
35 # include <linux/efi.h>
38 static inline unsigned long size_inside_page(unsigned long start
,
43 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
49 * Architectures vary in how they handle caching for addresses
50 * outside of main memory.
53 static inline int uncached_access(struct file
*file
, unsigned long addr
)
55 #if defined(CONFIG_IA64)
57 * On ia64, we ignore O_DSYNC because we cannot tolerate memory attribute aliases.
59 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
60 #elif defined(CONFIG_MIPS)
62 extern int __uncached_access(struct file
*file
,
65 return __uncached_access(file
, addr
);
69 * Accessing memory above the top the kernel knows about or through a file pointer
70 * that was marked O_DSYNC will be done non-cached.
72 if (file
->f_flags
& O_DSYNC
)
74 return addr
>= __pa(high_memory
);
78 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
79 static inline int valid_phys_addr_range(unsigned long addr
, size_t count
)
81 if (addr
+ count
> __pa(high_memory
))
87 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
93 #ifdef CONFIG_STRICT_DEVMEM
94 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
96 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
100 while (cursor
< to
) {
101 if (!devmem_is_allowed(pfn
)) {
103 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
104 current
->comm
, from
, to
);
113 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
119 void __attribute__((weak
)) unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
124 * This funcion reads the *physical* memory. The f_pos points directly to the
127 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
128 size_t count
, loff_t
*ppos
)
130 unsigned long p
= *ppos
;
134 if (!valid_phys_addr_range(p
, count
))
137 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
138 /* we don't have page 0 mapped on sparc and m68k.. */
140 sz
= size_inside_page(p
, count
);
142 if (clear_user(buf
, sz
))
153 unsigned long remaining
;
155 sz
= size_inside_page(p
, count
);
157 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
))
161 * On ia64 if a page has been mapped somewhere as
162 * uncached, then it must also be accessed uncached
163 * by the kernel or data corruption may occur
165 ptr
= xlate_dev_mem_ptr(p
);
169 remaining
= copy_to_user(buf
, ptr
, sz
);
170 unxlate_dev_mem_ptr(p
, ptr
);
184 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
185 size_t count
, loff_t
*ppos
)
187 unsigned long p
= *ppos
;
189 unsigned long copied
;
192 if (!valid_phys_addr_range(p
, count
))
197 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
198 /* we don't have page 0 mapped on sparc and m68k.. */
200 sz
= size_inside_page(p
, count
);
201 /* Hmm. Do something? */
210 sz
= size_inside_page(p
, count
);
212 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
216 * On ia64 if a page has been mapped somewhere as
217 * uncached, then it must also be accessed uncached
218 * by the kernel or data corruption may occur
220 ptr
= xlate_dev_mem_ptr(p
);
227 copied
= copy_from_user(ptr
, buf
, sz
);
228 unxlate_dev_mem_ptr(p
, ptr
);
230 written
+= sz
- copied
;
246 int __attribute__((weak
)) phys_mem_access_prot_allowed(struct file
*file
,
247 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
252 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
253 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
254 unsigned long size
, pgprot_t vma_prot
)
256 #ifdef pgprot_noncached
257 unsigned long offset
= pfn
<< PAGE_SHIFT
;
259 if (uncached_access(file
, offset
))
260 return pgprot_noncached(vma_prot
);
267 static unsigned long get_unmapped_area_mem(struct file
*file
,
273 if (!valid_mmap_phys_addr_range(pgoff
, len
))
274 return (unsigned long) -EINVAL
;
275 return pgoff
<< PAGE_SHIFT
;
278 /* can't do an in-place private mapping if there's no MMU */
279 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
281 return vma
->vm_flags
& VM_MAYSHARE
;
284 #define get_unmapped_area_mem NULL
286 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
292 static const struct vm_operations_struct mmap_mem_ops
= {
293 #ifdef CONFIG_HAVE_IOREMAP_PROT
294 .access
= generic_access_phys
298 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
300 size_t size
= vma
->vm_end
- vma
->vm_start
;
302 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
305 if (!private_mapping_ok(vma
))
308 if (!range_is_allowed(vma
->vm_pgoff
, size
))
311 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
315 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
319 vma
->vm_ops
= &mmap_mem_ops
;
321 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
322 if (remap_pfn_range(vma
,
326 vma
->vm_page_prot
)) {
332 #ifdef CONFIG_DEVKMEM
333 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
337 /* Turn a kernel-virtual address into a physical page frame */
338 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
341 * RED-PEN: on some architectures there is more mapped memory
342 * than available in mem_map which pfn_valid checks
343 * for. Perhaps should add a new macro here.
345 * RED-PEN: vmalloc is not supported right now.
351 return mmap_mem(file
, vma
);
355 #ifdef CONFIG_CRASH_DUMP
357 * Read memory corresponding to the old kernel.
359 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
360 size_t count
, loff_t
*ppos
)
362 unsigned long pfn
, offset
;
363 size_t read
= 0, csize
;
367 pfn
= *ppos
/ PAGE_SIZE
;
368 if (pfn
> saved_max_pfn
)
371 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
372 if (count
> PAGE_SIZE
- offset
)
373 csize
= PAGE_SIZE
- offset
;
377 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
389 #ifdef CONFIG_DEVKMEM
391 * This function reads the *virtual* memory as seen by the kernel.
393 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
394 size_t count
, loff_t
*ppos
)
396 unsigned long p
= *ppos
;
397 ssize_t low_count
, read
, sz
;
398 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
401 if (p
< (unsigned long) high_memory
) {
403 if (count
> (unsigned long) high_memory
- p
)
404 low_count
= (unsigned long) high_memory
- p
;
406 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
407 /* we don't have page 0 mapped on sparc and m68k.. */
408 if (p
< PAGE_SIZE
&& low_count
> 0) {
409 sz
= size_inside_page(p
, low_count
);
410 if (clear_user(buf
, sz
))
419 while (low_count
> 0) {
420 sz
= size_inside_page(p
, low_count
);
423 * On ia64 if a page has been mapped somewhere as
424 * uncached, then it must also be accessed uncached
425 * by the kernel or data corruption may occur
427 kbuf
= xlate_dev_kmem_ptr((char *)p
);
429 if (copy_to_user(buf
, kbuf
, sz
))
440 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
444 sz
= size_inside_page(p
, count
);
445 sz
= vread(kbuf
, (char *)p
, sz
);
448 if (copy_to_user(buf
, kbuf
, sz
)) {
449 free_page((unsigned long)kbuf
);
457 free_page((unsigned long)kbuf
);
464 static inline ssize_t
465 do_write_kmem(unsigned long p
, const char __user
*buf
,
466 size_t count
, loff_t
*ppos
)
469 unsigned long copied
;
472 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
473 /* we don't have page 0 mapped on sparc and m68k.. */
475 sz
= size_inside_page(p
, count
);
476 /* Hmm. Do something? */
487 sz
= size_inside_page(p
, count
);
490 * On ia64 if a page has been mapped somewhere as
491 * uncached, then it must also be accessed uncached
492 * by the kernel or data corruption may occur
494 ptr
= xlate_dev_kmem_ptr((char *)p
);
496 copied
= copy_from_user(ptr
, buf
, sz
);
498 written
+= sz
- copied
;
515 * This function writes to the *virtual* memory as seen by the kernel.
517 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
518 size_t count
, loff_t
*ppos
)
520 unsigned long p
= *ppos
;
523 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
525 if (p
< (unsigned long) high_memory
) {
526 unsigned long to_write
= min_t(unsigned long, count
,
527 (unsigned long)high_memory
- p
);
528 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
529 if (wrote
!= to_write
)
537 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
539 return wrote
? wrote
: -ENOMEM
;
541 unsigned long sz
= size_inside_page(p
, count
);
544 n
= copy_from_user(kbuf
, buf
, sz
);
548 free_page((unsigned long)kbuf
);
551 sz
= vwrite(kbuf
, (char *)p
, sz
);
557 free_page((unsigned long)kbuf
);
561 return virtr
+ wrote
;
565 #ifdef CONFIG_DEVPORT
566 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
567 size_t count
, loff_t
*ppos
)
569 unsigned long i
= *ppos
;
570 char __user
*tmp
= buf
;
572 if (!access_ok(VERIFY_WRITE
, buf
, count
))
574 while (count
-- > 0 && i
< 65536) {
575 if (__put_user(inb(i
),tmp
) < 0)
584 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
585 size_t count
, loff_t
*ppos
)
587 unsigned long i
= *ppos
;
588 const char __user
* tmp
= buf
;
590 if (!access_ok(VERIFY_READ
,buf
,count
))
592 while (count
-- > 0 && i
< 65536) {
594 if (__get_user(c
, tmp
)) {
608 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
609 size_t count
, loff_t
*ppos
)
614 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
615 size_t count
, loff_t
*ppos
)
620 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
621 struct splice_desc
*sd
)
626 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
627 loff_t
*ppos
, size_t len
, unsigned int flags
)
629 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
632 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
633 size_t count
, loff_t
*ppos
)
640 if (!access_ok(VERIFY_WRITE
, buf
, count
))
645 unsigned long unwritten
;
646 size_t chunk
= count
;
648 if (chunk
> PAGE_SIZE
)
649 chunk
= PAGE_SIZE
; /* Just for latency reasons */
650 unwritten
= __clear_user(buf
, chunk
);
651 written
+= chunk
- unwritten
;
654 if (signal_pending(current
))
655 return written
? written
: -ERESTARTSYS
;
660 return written
? written
: -EFAULT
;
663 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
668 if (vma
->vm_flags
& VM_SHARED
)
669 return shmem_zero_setup(vma
);
673 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
674 size_t count
, loff_t
*ppos
)
680 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
681 * can fopen() both devices with "a" now. This was previously impossible.
685 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
687 return file
->f_pos
= 0;
691 * The memory devices use the full 32/64 bits of the offset, and so we cannot
692 * check against negative addresses: they are ok. The return value is weird,
693 * though, in that case (0).
695 * also note that seeking relative to the "end of file" isn't supported:
696 * it has no meaning, so it returns -EINVAL.
698 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
702 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
705 file
->f_pos
= offset
;
707 force_successful_syscall_return();
710 file
->f_pos
+= offset
;
712 force_successful_syscall_return();
717 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
721 static int open_port(struct inode
* inode
, struct file
* filp
)
723 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
726 #define zero_lseek null_lseek
727 #define full_lseek null_lseek
728 #define write_zero write_null
729 #define read_full read_zero
730 #define open_mem open_port
731 #define open_kmem open_mem
732 #define open_oldmem open_mem
734 static const struct file_operations mem_fops
= {
735 .llseek
= memory_lseek
,
740 .get_unmapped_area
= get_unmapped_area_mem
,
743 #ifdef CONFIG_DEVKMEM
744 static const struct file_operations kmem_fops
= {
745 .llseek
= memory_lseek
,
750 .get_unmapped_area
= get_unmapped_area_mem
,
754 static const struct file_operations null_fops
= {
755 .llseek
= null_lseek
,
758 .splice_write
= splice_write_null
,
761 #ifdef CONFIG_DEVPORT
762 static const struct file_operations port_fops
= {
763 .llseek
= memory_lseek
,
770 static const struct file_operations zero_fops
= {
771 .llseek
= zero_lseek
,
778 * capabilities for /dev/zero
779 * - permits private mappings, "copies" are taken of the source of zeros
781 static struct backing_dev_info zero_bdi
= {
783 .capabilities
= BDI_CAP_MAP_COPY
,
786 static const struct file_operations full_fops
= {
787 .llseek
= full_lseek
,
792 #ifdef CONFIG_CRASH_DUMP
793 static const struct file_operations oldmem_fops
= {
799 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
800 size_t count
, loff_t
*ppos
)
805 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
809 if (!copy_from_user(tmp
, buf
, count
)) {
811 ret
= printk("%s", tmp
);
813 /* printk can add a prefix */
820 static const struct file_operations kmsg_fops
= {
824 static const struct memdev
{
827 const struct file_operations
*fops
;
828 struct backing_dev_info
*dev_info
;
830 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
831 #ifdef CONFIG_DEVKMEM
832 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
834 [3] = { "null", 0666, &null_fops
, NULL
},
835 #ifdef CONFIG_DEVPORT
836 [4] = { "port", 0, &port_fops
, NULL
},
838 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
839 [7] = { "full", 0666, &full_fops
, NULL
},
840 [8] = { "random", 0666, &random_fops
, NULL
},
841 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
842 [11] = { "kmsg", 0, &kmsg_fops
, NULL
},
843 #ifdef CONFIG_CRASH_DUMP
844 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
848 static int memory_open(struct inode
*inode
, struct file
*filp
)
851 const struct memdev
*dev
;
856 minor
= iminor(inode
);
857 if (minor
>= ARRAY_SIZE(devlist
))
860 dev
= &devlist
[minor
];
864 filp
->f_op
= dev
->fops
;
866 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
869 ret
= dev
->fops
->open(inode
, filp
);
877 static const struct file_operations memory_fops
= {
881 static char *mem_devnode(struct device
*dev
, mode_t
*mode
)
883 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
884 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
888 static struct class *mem_class
;
890 static int __init
chr_dev_init(void)
895 err
= bdi_init(&zero_bdi
);
899 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
900 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
902 mem_class
= class_create(THIS_MODULE
, "mem");
903 mem_class
->devnode
= mem_devnode
;
904 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
905 if (!devlist
[minor
].name
)
907 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
908 NULL
, devlist
[minor
].name
);
914 fs_initcall(chr_dev_init
);