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/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/export.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
;
103 if (!valid_phys_addr_range(p
, count
))
106 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
107 /* we don't have page 0 mapped on sparc and m68k.. */
109 sz
= size_inside_page(p
, count
);
111 if (clear_user(buf
, sz
))
122 unsigned long remaining
;
124 sz
= size_inside_page(p
, count
);
126 if (!range_is_allowed(p
>> PAGE_SHIFT
, count
))
130 * On ia64 if a page has been mapped somewhere as uncached, then
131 * it must also be accessed uncached by the kernel or data
132 * corruption may occur.
134 ptr
= xlate_dev_mem_ptr(p
);
138 remaining
= copy_to_user(buf
, ptr
, sz
);
139 unxlate_dev_mem_ptr(p
, ptr
);
153 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
154 size_t count
, loff_t
*ppos
)
156 phys_addr_t p
= *ppos
;
158 unsigned long copied
;
161 if (!valid_phys_addr_range(p
, count
))
166 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
167 /* we don't have page 0 mapped on sparc and m68k.. */
169 sz
= size_inside_page(p
, count
);
170 /* Hmm. Do something? */
179 sz
= size_inside_page(p
, count
);
181 if (!range_is_allowed(p
>> PAGE_SHIFT
, sz
))
185 * On ia64 if a page has been mapped somewhere as uncached, then
186 * it must also be accessed uncached by the kernel or data
187 * corruption may occur.
189 ptr
= xlate_dev_mem_ptr(p
);
196 copied
= copy_from_user(ptr
, buf
, sz
);
197 unxlate_dev_mem_ptr(p
, ptr
);
199 written
+= sz
- copied
;
215 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
216 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
221 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
224 * Architectures vary in how they handle caching for addresses
225 * outside of main memory.
228 #ifdef pgprot_noncached
229 static int uncached_access(struct file
*file
, phys_addr_t addr
)
231 #if defined(CONFIG_IA64)
233 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
236 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
237 #elif defined(CONFIG_MIPS)
239 extern int __uncached_access(struct file
*file
,
242 return __uncached_access(file
, addr
);
246 * Accessing memory above the top the kernel knows about or through a
248 * that was marked O_DSYNC will be done non-cached.
250 if (file
->f_flags
& O_DSYNC
)
252 return addr
>= __pa(high_memory
);
257 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
258 unsigned long size
, pgprot_t vma_prot
)
260 #ifdef pgprot_noncached
261 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
263 if (uncached_access(file
, offset
))
264 return pgprot_noncached(vma_prot
);
271 static unsigned long get_unmapped_area_mem(struct file
*file
,
277 if (!valid_mmap_phys_addr_range(pgoff
, len
))
278 return (unsigned long) -EINVAL
;
279 return pgoff
<< PAGE_SHIFT
;
282 /* can't do an in-place private mapping if there's no MMU */
283 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
285 return vma
->vm_flags
& VM_MAYSHARE
;
288 #define get_unmapped_area_mem NULL
290 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
296 static const struct vm_operations_struct mmap_mem_ops
= {
297 #ifdef CONFIG_HAVE_IOREMAP_PROT
298 .access
= generic_access_phys
302 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
304 size_t size
= vma
->vm_end
- vma
->vm_start
;
306 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
309 if (!private_mapping_ok(vma
))
312 if (!range_is_allowed(vma
->vm_pgoff
, size
))
315 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
319 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
323 vma
->vm_ops
= &mmap_mem_ops
;
325 /* Remap-pfn-range will mark the range VM_IO */
326 if (remap_pfn_range(vma
,
330 vma
->vm_page_prot
)) {
336 #ifdef CONFIG_DEVKMEM
337 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
341 /* Turn a kernel-virtual address into a physical page frame */
342 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
345 * RED-PEN: on some architectures there is more mapped memory than
346 * available in mem_map which pfn_valid checks for. Perhaps should add a
349 * RED-PEN: vmalloc is not supported right now.
355 return mmap_mem(file
, vma
);
359 #ifdef CONFIG_CRASH_DUMP
361 * Read memory corresponding to the old kernel.
363 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
364 size_t count
, loff_t
*ppos
)
366 unsigned long pfn
, offset
;
367 size_t read
= 0, csize
;
371 pfn
= *ppos
/ PAGE_SIZE
;
372 if (pfn
> saved_max_pfn
)
375 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
376 if (count
> PAGE_SIZE
- offset
)
377 csize
= PAGE_SIZE
- offset
;
381 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
393 #ifdef CONFIG_DEVKMEM
395 * This function reads the *virtual* memory as seen by the kernel.
397 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
398 size_t count
, loff_t
*ppos
)
400 unsigned long p
= *ppos
;
401 ssize_t low_count
, read
, sz
;
402 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
406 if (p
< (unsigned long) high_memory
) {
408 if (count
> (unsigned long)high_memory
- p
)
409 low_count
= (unsigned long)high_memory
- p
;
411 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
412 /* we don't have page 0 mapped on sparc and m68k.. */
413 if (p
< PAGE_SIZE
&& low_count
> 0) {
414 sz
= size_inside_page(p
, low_count
);
415 if (clear_user(buf
, sz
))
424 while (low_count
> 0) {
425 sz
= size_inside_page(p
, low_count
);
428 * On ia64 if a page has been mapped somewhere as
429 * uncached, then it must also be accessed uncached
430 * by the kernel or data corruption may occur
432 kbuf
= xlate_dev_kmem_ptr((char *)p
);
434 if (copy_to_user(buf
, kbuf
, sz
))
445 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
449 sz
= size_inside_page(p
, count
);
450 if (!is_vmalloc_or_module_addr((void *)p
)) {
454 sz
= vread(kbuf
, (char *)p
, sz
);
457 if (copy_to_user(buf
, kbuf
, sz
)) {
466 free_page((unsigned long)kbuf
);
469 return read
? read
: err
;
473 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
474 size_t count
, loff_t
*ppos
)
477 unsigned long copied
;
480 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
481 /* we don't have page 0 mapped on sparc and m68k.. */
483 sz
= size_inside_page(p
, count
);
484 /* Hmm. Do something? */
495 sz
= size_inside_page(p
, count
);
498 * On ia64 if a page has been mapped somewhere as uncached, then
499 * it must also be accessed uncached by the kernel or data
500 * corruption may occur.
502 ptr
= xlate_dev_kmem_ptr((char *)p
);
504 copied
= copy_from_user(ptr
, buf
, sz
);
506 written
+= sz
- copied
;
522 * This function writes to the *virtual* memory as seen by the kernel.
524 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
525 size_t count
, loff_t
*ppos
)
527 unsigned long p
= *ppos
;
530 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
533 if (p
< (unsigned long) high_memory
) {
534 unsigned long to_write
= min_t(unsigned long, count
,
535 (unsigned long)high_memory
- p
);
536 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
537 if (wrote
!= to_write
)
545 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
547 return wrote
? wrote
: -ENOMEM
;
549 unsigned long sz
= size_inside_page(p
, count
);
552 if (!is_vmalloc_or_module_addr((void *)p
)) {
556 n
= copy_from_user(kbuf
, buf
, sz
);
561 vwrite(kbuf
, (char *)p
, sz
);
567 free_page((unsigned long)kbuf
);
571 return virtr
+ wrote
? : err
;
575 #ifdef CONFIG_DEVPORT
576 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
577 size_t count
, loff_t
*ppos
)
579 unsigned long i
= *ppos
;
580 char __user
*tmp
= buf
;
582 if (!access_ok(VERIFY_WRITE
, buf
, count
))
584 while (count
-- > 0 && i
< 65536) {
585 if (__put_user(inb(i
), tmp
) < 0)
594 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
595 size_t count
, loff_t
*ppos
)
597 unsigned long i
= *ppos
;
598 const char __user
* tmp
= buf
;
600 if (!access_ok(VERIFY_READ
, buf
, count
))
602 while (count
-- > 0 && i
< 65536) {
604 if (__get_user(c
, tmp
)) {
618 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
619 size_t count
, loff_t
*ppos
)
624 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
625 size_t count
, loff_t
*ppos
)
630 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
631 struct splice_desc
*sd
)
636 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
637 loff_t
*ppos
, size_t len
, unsigned int flags
)
639 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
642 static ssize_t
read_zero(struct file
*file
, char __user
*buf
,
643 size_t count
, loff_t
*ppos
)
650 if (!access_ok(VERIFY_WRITE
, buf
, count
))
655 unsigned long unwritten
;
656 size_t chunk
= count
;
658 if (chunk
> PAGE_SIZE
)
659 chunk
= PAGE_SIZE
; /* Just for latency reasons */
660 unwritten
= __clear_user(buf
, chunk
);
661 written
+= chunk
- unwritten
;
664 if (signal_pending(current
))
665 return written
? written
: -ERESTARTSYS
;
670 return written
? written
: -EFAULT
;
673 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
678 if (vma
->vm_flags
& VM_SHARED
)
679 return shmem_zero_setup(vma
);
683 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
684 size_t count
, loff_t
*ppos
)
690 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
691 * can fopen() both devices with "a" now. This was previously impossible.
694 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
696 return file
->f_pos
= 0;
700 * The memory devices use the full 32/64 bits of the offset, and so we cannot
701 * check against negative addresses: they are ok. The return value is weird,
702 * though, in that case (0).
704 * also note that seeking relative to the "end of file" isn't supported:
705 * it has no meaning, so it returns -EINVAL.
707 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
711 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
714 offset
+= file
->f_pos
;
716 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
717 if ((unsigned long long)offset
>= ~0xFFFULL
) {
721 file
->f_pos
= offset
;
723 force_successful_syscall_return();
728 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
732 static int open_port(struct inode
* inode
, struct file
* filp
)
734 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
737 #define zero_lseek null_lseek
738 #define full_lseek null_lseek
739 #define write_zero write_null
740 #define read_full read_zero
741 #define open_mem open_port
742 #define open_kmem open_mem
743 #define open_oldmem open_mem
745 static const struct file_operations mem_fops
= {
746 .llseek
= memory_lseek
,
751 .get_unmapped_area
= get_unmapped_area_mem
,
754 #ifdef CONFIG_DEVKMEM
755 static const struct file_operations kmem_fops
= {
756 .llseek
= memory_lseek
,
761 .get_unmapped_area
= get_unmapped_area_mem
,
765 static const struct file_operations null_fops
= {
766 .llseek
= null_lseek
,
769 .splice_write
= splice_write_null
,
772 #ifdef CONFIG_DEVPORT
773 static const struct file_operations port_fops
= {
774 .llseek
= memory_lseek
,
781 static const struct file_operations zero_fops
= {
782 .llseek
= zero_lseek
,
789 * capabilities for /dev/zero
790 * - permits private mappings, "copies" are taken of the source of zeros
791 * - no writeback happens
793 static struct backing_dev_info zero_bdi
= {
795 .capabilities
= BDI_CAP_MAP_COPY
| BDI_CAP_NO_ACCT_AND_WRITEBACK
,
798 static const struct file_operations full_fops
= {
799 .llseek
= full_lseek
,
804 #ifdef CONFIG_CRASH_DUMP
805 static const struct file_operations oldmem_fops
= {
808 .llseek
= default_llseek
,
812 static const struct memdev
{
815 const struct file_operations
*fops
;
816 struct backing_dev_info
*dev_info
;
818 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
819 #ifdef CONFIG_DEVKMEM
820 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
822 [3] = { "null", 0666, &null_fops
, NULL
},
823 #ifdef CONFIG_DEVPORT
824 [4] = { "port", 0, &port_fops
, NULL
},
826 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
827 [7] = { "full", 0666, &full_fops
, NULL
},
828 [8] = { "random", 0666, &random_fops
, NULL
},
829 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
831 [11] = { "kmsg", 0644, &kmsg_fops
, NULL
},
833 #ifdef CONFIG_CRASH_DUMP
834 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
838 static int memory_open(struct inode
*inode
, struct file
*filp
)
841 const struct memdev
*dev
;
843 minor
= iminor(inode
);
844 if (minor
>= ARRAY_SIZE(devlist
))
847 dev
= &devlist
[minor
];
851 filp
->f_op
= dev
->fops
;
853 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
855 /* Is /dev/mem or /dev/kmem ? */
856 if (dev
->dev_info
== &directly_mappable_cdev_bdi
)
857 filp
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
860 return dev
->fops
->open(inode
, filp
);
865 static const struct file_operations memory_fops
= {
867 .llseek
= noop_llseek
,
870 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
872 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
873 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
877 static struct class *mem_class
;
879 static int __init
chr_dev_init(void)
884 err
= bdi_init(&zero_bdi
);
888 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
889 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
891 mem_class
= class_create(THIS_MODULE
, "mem");
892 if (IS_ERR(mem_class
))
893 return PTR_ERR(mem_class
);
895 mem_class
->devnode
= mem_devnode
;
896 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
897 if (!devlist
[minor
].name
)
903 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
906 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
907 NULL
, devlist
[minor
].name
);
913 fs_initcall(chr_dev_init
);