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 if (-start
& (PAGE_SIZE
- 1))
44 sz
= -start
& (PAGE_SIZE
- 1);
48 return min_t(unsigned long, sz
, size
);
52 * Architectures vary in how they handle caching for addresses
53 * outside of main memory.
56 static inline int uncached_access(struct file
*file
, unsigned long addr
)
58 #if defined(CONFIG_IA64)
60 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
62 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
63 #elif defined(CONFIG_MIPS)
65 extern int __uncached_access(struct file
*file
,
68 return __uncached_access(file
, addr
);
72 * Accessing memory above the top the kernel knows about or through a file pointer
73 * that was marked O_SYNC will be done non-cached.
75 if (file
->f_flags
& O_SYNC
)
77 return addr
>= __pa(high_memory
);
81 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
82 static inline int valid_phys_addr_range(unsigned long addr
, size_t count
)
84 if (addr
+ count
> __pa(high_memory
))
90 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
96 #ifdef CONFIG_STRICT_DEVMEM
97 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
99 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
100 u64 to
= from
+ size
;
103 while (cursor
< to
) {
104 if (!devmem_is_allowed(pfn
)) {
106 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
107 current
->comm
, from
, to
);
116 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
122 void __attribute__((weak
)) unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
127 * This funcion reads the *physical* memory. The f_pos points directly to the
130 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
131 size_t count
, loff_t
*ppos
)
133 unsigned long p
= *ppos
;
137 if (!valid_phys_addr_range(p
, count
))
140 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
141 /* we don't have page 0 mapped on sparc and m68k.. */
147 if (clear_user(buf
, sz
))
159 * Handle first page in case it's not aligned
161 if (-p
& (PAGE_SIZE
- 1))
162 sz
= -p
& (PAGE_SIZE
- 1);
166 sz
= min_t(unsigned long, sz
, count
);
168 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
))
172 * On ia64 if a page has been mapped somewhere as
173 * uncached, then it must also be accessed uncached
174 * by the kernel or data corruption may occur
176 ptr
= xlate_dev_mem_ptr(p
);
180 if (copy_to_user(buf
, ptr
, sz
)) {
181 unxlate_dev_mem_ptr(p
, ptr
);
185 unxlate_dev_mem_ptr(p
, ptr
);
197 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
198 size_t count
, loff_t
*ppos
)
200 unsigned long p
= *ppos
;
202 unsigned long copied
;
205 if (!valid_phys_addr_range(p
, count
))
210 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
211 /* we don't have page 0 mapped on sparc and m68k.. */
213 unsigned long sz
= PAGE_SIZE
- p
;
216 /* Hmm. Do something? */
226 * Handle first page in case it's not aligned
228 if (-p
& (PAGE_SIZE
- 1))
229 sz
= -p
& (PAGE_SIZE
- 1);
233 sz
= min_t(unsigned long, sz
, count
);
235 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
239 * On ia64 if a page has been mapped somewhere as
240 * uncached, then it must also be accessed uncached
241 * by the kernel or data corruption may occur
243 ptr
= xlate_dev_mem_ptr(p
);
250 copied
= copy_from_user(ptr
, buf
, sz
);
252 written
+= sz
- copied
;
253 unxlate_dev_mem_ptr(p
, ptr
);
259 unxlate_dev_mem_ptr(p
, ptr
);
271 int __attribute__((weak
)) phys_mem_access_prot_allowed(struct file
*file
,
272 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
277 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
278 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
279 unsigned long size
, pgprot_t vma_prot
)
281 #ifdef pgprot_noncached
282 unsigned long offset
= pfn
<< PAGE_SHIFT
;
284 if (uncached_access(file
, offset
))
285 return pgprot_noncached(vma_prot
);
292 static unsigned long get_unmapped_area_mem(struct file
*file
,
298 if (!valid_mmap_phys_addr_range(pgoff
, len
))
299 return (unsigned long) -EINVAL
;
300 return pgoff
<< PAGE_SHIFT
;
303 /* can't do an in-place private mapping if there's no MMU */
304 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
306 return vma
->vm_flags
& VM_MAYSHARE
;
309 #define get_unmapped_area_mem NULL
311 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
317 static const struct vm_operations_struct mmap_mem_ops
= {
318 #ifdef CONFIG_HAVE_IOREMAP_PROT
319 .access
= generic_access_phys
323 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
325 size_t size
= vma
->vm_end
- vma
->vm_start
;
327 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
330 if (!private_mapping_ok(vma
))
333 if (!range_is_allowed(vma
->vm_pgoff
, size
))
336 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
340 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
344 vma
->vm_ops
= &mmap_mem_ops
;
346 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
347 if (remap_pfn_range(vma
,
351 vma
->vm_page_prot
)) {
357 #ifdef CONFIG_DEVKMEM
358 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
362 /* Turn a kernel-virtual address into a physical page frame */
363 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
366 * RED-PEN: on some architectures there is more mapped memory
367 * than available in mem_map which pfn_valid checks
368 * for. Perhaps should add a new macro here.
370 * RED-PEN: vmalloc is not supported right now.
376 return mmap_mem(file
, vma
);
380 #ifdef CONFIG_CRASH_DUMP
382 * Read memory corresponding to the old kernel.
384 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
385 size_t count
, loff_t
*ppos
)
387 unsigned long pfn
, offset
;
388 size_t read
= 0, csize
;
392 pfn
= *ppos
/ PAGE_SIZE
;
393 if (pfn
> saved_max_pfn
)
396 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
397 if (count
> PAGE_SIZE
- offset
)
398 csize
= PAGE_SIZE
- offset
;
402 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
414 #ifdef CONFIG_DEVKMEM
416 * This function reads the *virtual* memory as seen by the kernel.
418 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
419 size_t count
, loff_t
*ppos
)
421 unsigned long p
= *ppos
;
422 ssize_t low_count
, read
, sz
;
423 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
427 if (p
< (unsigned long) high_memory
) {
429 if (count
> (unsigned long) high_memory
- p
)
430 low_count
= (unsigned long) high_memory
- p
;
432 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
433 /* we don't have page 0 mapped on sparc and m68k.. */
434 if (p
< PAGE_SIZE
&& low_count
> 0) {
435 size_t tmp
= PAGE_SIZE
- p
;
436 if (tmp
> low_count
) tmp
= low_count
;
437 if (clear_user(buf
, tmp
))
446 while (low_count
> 0) {
447 sz
= size_inside_page(p
, low_count
);
450 * On ia64 if a page has been mapped somewhere as
451 * uncached, then it must also be accessed uncached
452 * by the kernel or data corruption may occur
454 kbuf
= xlate_dev_kmem_ptr((char *)p
);
456 if (copy_to_user(buf
, kbuf
, sz
))
467 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
471 int len
= size_inside_page(p
, count
);
473 if (!is_vmalloc_or_module_addr((void *)p
)) {
477 len
= vread(kbuf
, (char *)p
, len
);
480 if (copy_to_user(buf
, kbuf
, len
)) {
489 free_page((unsigned long)kbuf
);
492 return read
? read
: err
;
496 static inline ssize_t
497 do_write_kmem(void *p
, unsigned long realp
, 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.. */
506 if (realp
< PAGE_SIZE
) {
507 unsigned long sz
= PAGE_SIZE
- realp
;
510 /* Hmm. Do something? */
522 sz
= size_inside_page(realp
, count
);
525 * On ia64 if a page has been mapped somewhere as
526 * uncached, then it must also be accessed uncached
527 * by the kernel or data corruption may occur
529 ptr
= xlate_dev_kmem_ptr(p
);
531 copied
= copy_from_user(ptr
, buf
, sz
);
533 written
+= sz
- copied
;
551 * This function writes to the *virtual* memory as seen by the kernel.
553 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
554 size_t count
, loff_t
*ppos
)
556 unsigned long p
= *ppos
;
560 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
563 if (p
< (unsigned long) high_memory
) {
566 if (count
> (unsigned long) high_memory
- p
)
567 wrote
= (unsigned long) high_memory
- p
;
569 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
570 if (written
!= wrote
)
579 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
581 return wrote
? wrote
: -ENOMEM
;
583 int len
= size_inside_page(p
, count
);
585 if (!is_vmalloc_or_module_addr((void *)p
)) {
590 written
= copy_from_user(kbuf
, buf
, len
);
596 vwrite(kbuf
, (char *)p
, len
);
602 free_page((unsigned long)kbuf
);
606 return virtr
+ wrote
? : err
;
610 #ifdef CONFIG_DEVPORT
611 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
612 size_t count
, loff_t
*ppos
)
614 unsigned long i
= *ppos
;
615 char __user
*tmp
= buf
;
617 if (!access_ok(VERIFY_WRITE
, buf
, count
))
619 while (count
-- > 0 && i
< 65536) {
620 if (__put_user(inb(i
),tmp
) < 0)
629 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
630 size_t count
, loff_t
*ppos
)
632 unsigned long i
= *ppos
;
633 const char __user
* tmp
= buf
;
635 if (!access_ok(VERIFY_READ
,buf
,count
))
637 while (count
-- > 0 && i
< 65536) {
639 if (__get_user(c
, tmp
)) {
653 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
654 size_t count
, loff_t
*ppos
)
659 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
660 size_t count
, loff_t
*ppos
)
665 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
666 struct splice_desc
*sd
)
671 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
,struct file
*out
,
672 loff_t
*ppos
, size_t len
, unsigned int flags
)
674 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
677 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
678 size_t count
, loff_t
*ppos
)
685 if (!access_ok(VERIFY_WRITE
, buf
, count
))
690 unsigned long unwritten
;
691 size_t chunk
= count
;
693 if (chunk
> PAGE_SIZE
)
694 chunk
= PAGE_SIZE
; /* Just for latency reasons */
695 unwritten
= __clear_user(buf
, chunk
);
696 written
+= chunk
- unwritten
;
699 if (signal_pending(current
))
700 return written
? written
: -ERESTARTSYS
;
705 return written
? written
: -EFAULT
;
708 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
713 if (vma
->vm_flags
& VM_SHARED
)
714 return shmem_zero_setup(vma
);
718 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
719 size_t count
, loff_t
*ppos
)
725 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
726 * can fopen() both devices with "a" now. This was previously impossible.
730 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
732 return file
->f_pos
= 0;
736 * The memory devices use the full 32/64 bits of the offset, and so we cannot
737 * check against negative addresses: they are ok. The return value is weird,
738 * though, in that case (0).
740 * also note that seeking relative to the "end of file" isn't supported:
741 * it has no meaning, so it returns -EINVAL.
743 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
747 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
750 file
->f_pos
= offset
;
752 force_successful_syscall_return();
755 file
->f_pos
+= offset
;
757 force_successful_syscall_return();
762 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
766 static int open_port(struct inode
* inode
, struct file
* filp
)
768 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
771 #define zero_lseek null_lseek
772 #define full_lseek null_lseek
773 #define write_zero write_null
774 #define read_full read_zero
775 #define open_mem open_port
776 #define open_kmem open_mem
777 #define open_oldmem open_mem
779 static const struct file_operations mem_fops
= {
780 .llseek
= memory_lseek
,
785 .get_unmapped_area
= get_unmapped_area_mem
,
788 #ifdef CONFIG_DEVKMEM
789 static const struct file_operations kmem_fops
= {
790 .llseek
= memory_lseek
,
795 .get_unmapped_area
= get_unmapped_area_mem
,
799 static const struct file_operations null_fops
= {
800 .llseek
= null_lseek
,
803 .splice_write
= splice_write_null
,
806 #ifdef CONFIG_DEVPORT
807 static const struct file_operations port_fops
= {
808 .llseek
= memory_lseek
,
815 static const struct file_operations zero_fops
= {
816 .llseek
= zero_lseek
,
823 * capabilities for /dev/zero
824 * - permits private mappings, "copies" are taken of the source of zeros
825 * - no writeback happens
827 static struct backing_dev_info zero_bdi
= {
829 .capabilities
= BDI_CAP_MAP_COPY
| BDI_CAP_NO_ACCT_AND_WRITEBACK
,
832 static const struct file_operations full_fops
= {
833 .llseek
= full_lseek
,
838 #ifdef CONFIG_CRASH_DUMP
839 static const struct file_operations oldmem_fops
= {
845 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
846 size_t count
, loff_t
*ppos
)
851 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
855 if (!copy_from_user(tmp
, buf
, count
)) {
857 ret
= printk("%s", tmp
);
859 /* printk can add a prefix */
866 static const struct file_operations kmsg_fops
= {
870 static const struct memdev
{
873 const struct file_operations
*fops
;
874 struct backing_dev_info
*dev_info
;
876 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
877 #ifdef CONFIG_DEVKMEM
878 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
880 [3] = { "null", 0666, &null_fops
, NULL
},
881 #ifdef CONFIG_DEVPORT
882 [4] = { "port", 0, &port_fops
, NULL
},
884 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
885 [7] = { "full", 0666, &full_fops
, NULL
},
886 [8] = { "random", 0666, &random_fops
, NULL
},
887 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
888 [11] = { "kmsg", 0, &kmsg_fops
, NULL
},
889 #ifdef CONFIG_CRASH_DUMP
890 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
894 static int memory_open(struct inode
*inode
, struct file
*filp
)
897 const struct memdev
*dev
;
902 minor
= iminor(inode
);
903 if (minor
>= ARRAY_SIZE(devlist
))
906 dev
= &devlist
[minor
];
910 filp
->f_op
= dev
->fops
;
912 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
915 ret
= dev
->fops
->open(inode
, filp
);
923 static const struct file_operations memory_fops
= {
927 static char *mem_devnode(struct device
*dev
, mode_t
*mode
)
929 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
930 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
934 static struct class *mem_class
;
936 static int __init
chr_dev_init(void)
941 err
= bdi_init(&zero_bdi
);
945 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
946 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
948 mem_class
= class_create(THIS_MODULE
, "mem");
949 mem_class
->devnode
= mem_devnode
;
950 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
951 if (!devlist
[minor
].name
)
953 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
954 NULL
, devlist
[minor
].name
);
960 fs_initcall(chr_dev_init
);