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>
31 #include <linux/aio.h>
33 #ifdef CONFIG_KNOX_KAP
34 #include <linux/knox_kap.h>
38 #include <linux/mst_ctrl.h>
41 #include <asm/uaccess.h>
44 # include <linux/efi.h>
47 #define DEVPORT_MINOR 4
49 static inline unsigned long size_inside_page(unsigned long start
,
54 sz
= PAGE_SIZE
- (start
& (PAGE_SIZE
- 1));
59 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
60 static inline int valid_phys_addr_range(phys_addr_t addr
, size_t count
)
62 return addr
+ count
<= __pa(high_memory
);
65 static inline int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
71 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
72 #ifdef CONFIG_STRICT_DEVMEM
73 static inline int page_is_allowed(unsigned long pfn
)
75 return devmem_is_allowed(pfn
);
77 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
79 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
84 if (!devmem_is_allowed(pfn
))
92 static inline int page_is_allowed(unsigned long pfn
)
96 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
104 void __weak
unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
109 * This funcion reads the *physical* memory. The f_pos points directly to the
112 static ssize_t
read_mem(struct file
*file
, char __user
*buf
,
113 size_t count
, loff_t
*ppos
)
115 phys_addr_t p
= *ppos
;
119 if (!valid_phys_addr_range(p
, count
))
122 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
123 /* we don't have page 0 mapped on sparc and m68k.. */
125 sz
= size_inside_page(p
, count
);
127 if (clear_user(buf
, sz
))
138 unsigned long remaining
;
141 sz
= size_inside_page(p
, count
);
143 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
147 /* Show zeros for restricted memory. */
148 remaining
= clear_user(buf
, sz
);
151 * On ia64 if a page has been mapped somewhere as
152 * uncached, then it must also be accessed uncached
153 * by the kernel or data corruption may occur.
155 ptr
= xlate_dev_mem_ptr(p
);
159 remaining
= copy_to_user(buf
, ptr
, sz
);
161 unxlate_dev_mem_ptr(p
, ptr
);
177 static ssize_t
write_mem(struct file
*file
, const char __user
*buf
,
178 size_t count
, loff_t
*ppos
)
180 phys_addr_t p
= *ppos
;
182 unsigned long copied
;
185 if (!valid_phys_addr_range(p
, count
))
190 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
191 /* we don't have page 0 mapped on sparc and m68k.. */
193 sz
= size_inside_page(p
, count
);
194 /* Hmm. Do something? */
205 sz
= size_inside_page(p
, count
);
207 allowed
= page_is_allowed(p
>> PAGE_SHIFT
);
211 /* Skip actual writing when a page is marked as restricted. */
214 * On ia64 if a page has been mapped somewhere as
215 * uncached, then it must also be accessed uncached
216 * by the kernel or data corruption may occur.
218 ptr
= xlate_dev_mem_ptr(p
);
225 copied
= copy_from_user(ptr
, buf
, sz
);
226 unxlate_dev_mem_ptr(p
, ptr
);
228 written
+= sz
- copied
;
244 #endif /* CONFIG_DEVMEM */
246 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM)
248 int __weak
phys_mem_access_prot_allowed(struct file
*file
,
249 unsigned long pfn
, unsigned long size
, pgprot_t
*vma_prot
)
254 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
257 * Architectures vary in how they handle caching for addresses
258 * outside of main memory.
261 #ifdef pgprot_noncached
262 static int uncached_access(struct file
*file
, phys_addr_t addr
)
264 #if defined(CONFIG_IA64)
266 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
269 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
270 #elif defined(CONFIG_MIPS)
272 extern int __uncached_access(struct file
*file
,
275 return __uncached_access(file
, addr
);
279 * Accessing memory above the top the kernel knows about or through a
281 * that was marked O_DSYNC will be done non-cached.
283 if (file
->f_flags
& O_DSYNC
)
285 return addr
>= __pa(high_memory
);
290 static pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
291 unsigned long size
, pgprot_t vma_prot
)
293 #ifdef pgprot_noncached
294 phys_addr_t offset
= pfn
<< PAGE_SHIFT
;
296 if (uncached_access(file
, offset
))
297 return pgprot_noncached(vma_prot
);
304 static unsigned long get_unmapped_area_mem(struct file
*file
,
310 if (!valid_mmap_phys_addr_range(pgoff
, len
))
311 return (unsigned long) -EINVAL
;
312 return pgoff
<< PAGE_SHIFT
;
315 /* can't do an in-place private mapping if there's no MMU */
316 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
318 return vma
->vm_flags
& VM_MAYSHARE
;
321 #define get_unmapped_area_mem NULL
323 static inline int private_mapping_ok(struct vm_area_struct
*vma
)
329 static const struct vm_operations_struct mmap_mem_ops
= {
330 #ifdef CONFIG_HAVE_IOREMAP_PROT
331 .access
= generic_access_phys
335 static int mmap_mem(struct file
*file
, struct vm_area_struct
*vma
)
337 size_t size
= vma
->vm_end
- vma
->vm_start
;
339 if (!valid_mmap_phys_addr_range(vma
->vm_pgoff
, size
))
342 if (!private_mapping_ok(vma
))
345 if (!range_is_allowed(vma
->vm_pgoff
, size
))
348 if (!phys_mem_access_prot_allowed(file
, vma
->vm_pgoff
, size
,
352 vma
->vm_page_prot
= phys_mem_access_prot(file
, vma
->vm_pgoff
,
356 vma
->vm_ops
= &mmap_mem_ops
;
358 /* Remap-pfn-range will mark the range VM_IO */
359 if (remap_pfn_range(vma
,
363 vma
->vm_page_prot
)) {
368 #endif /* CONFIG_DEVMEM */
370 #ifdef CONFIG_DEVKMEM
371 static int mmap_kmem(struct file
*file
, struct vm_area_struct
*vma
)
375 /* Turn a kernel-virtual address into a physical page frame */
376 pfn
= __pa((u64
)vma
->vm_pgoff
<< PAGE_SHIFT
) >> PAGE_SHIFT
;
379 * RED-PEN: on some architectures there is more mapped memory than
380 * available in mem_map which pfn_valid checks for. Perhaps should add a
383 * RED-PEN: vmalloc is not supported right now.
389 return mmap_mem(file
, vma
);
393 #ifdef CONFIG_CRASH_DUMP
395 * Read memory corresponding to the old kernel.
397 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
398 size_t count
, loff_t
*ppos
)
400 unsigned long pfn
, offset
;
401 size_t read
= 0, csize
;
405 pfn
= *ppos
/ PAGE_SIZE
;
406 if (pfn
> saved_max_pfn
)
409 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
410 if (count
> PAGE_SIZE
- offset
)
411 csize
= PAGE_SIZE
- offset
;
415 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
427 #ifdef CONFIG_DEVKMEM
429 * This function reads the *virtual* memory as seen by the kernel.
431 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
432 size_t count
, loff_t
*ppos
)
434 unsigned long p
= *ppos
;
435 ssize_t low_count
, read
, sz
;
436 char *kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
440 if (p
< (unsigned long) high_memory
) {
442 if (count
> (unsigned long)high_memory
- p
)
443 low_count
= (unsigned long)high_memory
- p
;
445 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
446 /* we don't have page 0 mapped on sparc and m68k.. */
447 if (p
< PAGE_SIZE
&& low_count
> 0) {
448 sz
= size_inside_page(p
, low_count
);
449 if (clear_user(buf
, sz
))
458 while (low_count
> 0) {
459 sz
= size_inside_page(p
, low_count
);
462 * On ia64 if a page has been mapped somewhere as
463 * uncached, then it must also be accessed uncached
464 * by the kernel or data corruption may occur
466 kbuf
= xlate_dev_kmem_ptr((char *)p
);
468 if (copy_to_user(buf
, kbuf
, sz
))
479 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
483 sz
= size_inside_page(p
, count
);
484 if (!is_vmalloc_or_module_addr((void *)p
)) {
488 sz
= vread(kbuf
, (char *)p
, sz
);
491 if (copy_to_user(buf
, kbuf
, sz
)) {
500 free_page((unsigned long)kbuf
);
503 return read
? read
: err
;
507 static ssize_t
do_write_kmem(unsigned long p
, const char __user
*buf
,
508 size_t count
, loff_t
*ppos
)
511 unsigned long copied
;
514 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
515 /* we don't have page 0 mapped on sparc and m68k.. */
517 sz
= size_inside_page(p
, count
);
518 /* Hmm. Do something? */
529 sz
= size_inside_page(p
, count
);
532 * On ia64 if a page has been mapped somewhere as uncached, then
533 * it must also be accessed uncached by the kernel or data
534 * corruption may occur.
536 ptr
= xlate_dev_kmem_ptr((char *)p
);
538 copied
= copy_from_user(ptr
, buf
, sz
);
540 written
+= sz
- copied
;
556 * This function writes to the *virtual* memory as seen by the kernel.
558 static ssize_t
write_kmem(struct file
*file
, const char __user
*buf
,
559 size_t count
, loff_t
*ppos
)
561 unsigned long p
= *ppos
;
564 char *kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
567 if (p
< (unsigned long) high_memory
) {
568 unsigned long to_write
= min_t(unsigned long, count
,
569 (unsigned long)high_memory
- p
);
570 wrote
= do_write_kmem(p
, buf
, to_write
, ppos
);
571 if (wrote
!= to_write
)
579 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
581 return wrote
? wrote
: -ENOMEM
;
583 unsigned long sz
= size_inside_page(p
, count
);
586 if (!is_vmalloc_or_module_addr((void *)p
)) {
590 n
= copy_from_user(kbuf
, buf
, sz
);
595 vwrite(kbuf
, (char *)p
, sz
);
601 free_page((unsigned long)kbuf
);
605 return virtr
+ wrote
? : err
;
609 #ifdef CONFIG_DEVPORT
610 static ssize_t
read_port(struct file
*file
, char __user
*buf
,
611 size_t count
, loff_t
*ppos
)
613 unsigned long i
= *ppos
;
614 char __user
*tmp
= buf
;
616 if (!access_ok(VERIFY_WRITE
, buf
, count
))
618 while (count
-- > 0 && i
< 65536) {
619 if (__put_user(inb(i
), tmp
) < 0)
628 static ssize_t
write_port(struct file
*file
, const char __user
*buf
,
629 size_t count
, loff_t
*ppos
)
631 unsigned long i
= *ppos
;
632 const char __user
*tmp
= buf
;
634 if (!access_ok(VERIFY_READ
, buf
, count
))
636 while (count
-- > 0 && i
< 65536) {
638 if (__get_user(c
, tmp
)) {
652 static ssize_t
read_null(struct file
*file
, char __user
*buf
,
653 size_t count
, loff_t
*ppos
)
658 static ssize_t
write_null(struct file
*file
, const char __user
*buf
,
659 size_t count
, loff_t
*ppos
)
664 static ssize_t
aio_read_null(struct kiocb
*iocb
, const struct iovec
*iov
,
665 unsigned long nr_segs
, loff_t pos
)
670 static ssize_t
aio_write_null(struct kiocb
*iocb
, const struct iovec
*iov
,
671 unsigned long nr_segs
, loff_t pos
)
673 return iov_length(iov
, nr_segs
);
676 static int pipe_to_null(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
,
677 struct splice_desc
*sd
)
682 static ssize_t
splice_write_null(struct pipe_inode_info
*pipe
, struct file
*out
,
683 loff_t
*ppos
, size_t len
, unsigned int flags
)
685 return splice_from_pipe(pipe
, out
, ppos
, len
, flags
, pipe_to_null
);
688 static ssize_t
read_zero(struct file
*file
, char __user
*buf
,
689 size_t count
, loff_t
*ppos
)
696 if (!access_ok(VERIFY_WRITE
, buf
, count
))
701 unsigned long unwritten
;
702 size_t chunk
= count
;
704 if (chunk
> PAGE_SIZE
)
705 chunk
= PAGE_SIZE
; /* Just for latency reasons */
706 unwritten
= __clear_user(buf
, chunk
);
707 written
+= chunk
- unwritten
;
710 if (signal_pending(current
))
711 return written
? written
: -ERESTARTSYS
;
716 return written
? written
: -EFAULT
;
719 static ssize_t
aio_read_zero(struct kiocb
*iocb
, const struct iovec
*iov
,
720 unsigned long nr_segs
, loff_t pos
)
726 for (i
= 0; i
< nr_segs
; i
++) {
727 ret
= read_zero(iocb
->ki_filp
, iov
[i
].iov_base
, iov
[i
].iov_len
,
734 return written
? written
: -EFAULT
;
737 static int mmap_zero(struct file
*file
, struct vm_area_struct
*vma
)
742 if (vma
->vm_flags
& VM_SHARED
)
743 return shmem_zero_setup(vma
);
747 static ssize_t
write_full(struct file
*file
, const char __user
*buf
,
748 size_t count
, loff_t
*ppos
)
754 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
755 * can fopen() both devices with "a" now. This was previously impossible.
758 static loff_t
null_lseek(struct file
*file
, loff_t offset
, int orig
)
760 return file
->f_pos
= 0;
763 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
766 * The memory devices use the full 32/64 bits of the offset, and so we cannot
767 * check against negative addresses: they are ok. The return value is weird,
768 * though, in that case (0).
770 * also note that seeking relative to the "end of file" isn't supported:
771 * it has no meaning, so it returns -EINVAL.
773 static loff_t
memory_lseek(struct file
*file
, loff_t offset
, int orig
)
777 mutex_lock(&file_inode(file
)->i_mutex
);
780 offset
+= file
->f_pos
;
782 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
783 if ((unsigned long long)offset
>= ~0xFFFULL
) {
787 file
->f_pos
= offset
;
789 force_successful_syscall_return();
794 mutex_unlock(&file_inode(file
)->i_mutex
);
800 #if defined(CONFIG_DEVMEM) || defined(CONFIG_DEVKMEM) || defined(CONFIG_DEVPORT)
801 static int open_port(struct inode
*inode
, struct file
*filp
)
803 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
807 #define zero_lseek null_lseek
808 #define full_lseek null_lseek
809 #define write_zero write_null
810 #define read_full read_zero
811 #define aio_write_zero aio_write_null
812 #define open_mem open_port
813 #define open_kmem open_mem
814 #define open_oldmem open_mem
817 static const struct file_operations mem_fops
= {
818 .llseek
= memory_lseek
,
823 .get_unmapped_area
= get_unmapped_area_mem
,
827 #ifdef CONFIG_DEVKMEM
828 static const struct file_operations kmem_fops
= {
829 .llseek
= memory_lseek
,
834 .get_unmapped_area
= get_unmapped_area_mem
,
838 static const struct file_operations null_fops
= {
839 .llseek
= null_lseek
,
842 .aio_read
= aio_read_null
,
843 .aio_write
= aio_write_null
,
844 .splice_write
= splice_write_null
,
847 #ifdef CONFIG_DEVPORT
848 static const struct file_operations port_fops
= {
849 .llseek
= memory_lseek
,
856 static const struct file_operations zero_fops
= {
857 .llseek
= zero_lseek
,
860 .aio_read
= aio_read_zero
,
861 .aio_write
= aio_write_zero
,
866 * capabilities for /dev/zero
867 * - permits private mappings, "copies" are taken of the source of zeros
868 * - no writeback happens
870 static struct backing_dev_info zero_bdi
= {
872 .capabilities
= BDI_CAP_MAP_COPY
| BDI_CAP_NO_ACCT_AND_WRITEBACK
,
875 static const struct file_operations full_fops
= {
876 .llseek
= full_lseek
,
881 #ifdef CONFIG_CRASH_DUMP
882 static const struct file_operations oldmem_fops
= {
885 .llseek
= default_llseek
,
889 static const struct memdev
{
892 const struct file_operations
*fops
;
893 struct backing_dev_info
*dev_info
;
896 [1] = { "mem", 0, &mem_fops
, &directly_mappable_cdev_bdi
},
898 #ifdef CONFIG_DEVKMEM
899 [2] = { "kmem", 0, &kmem_fops
, &directly_mappable_cdev_bdi
},
901 [3] = { "null", 0666, &null_fops
, NULL
},
902 #ifdef CONFIG_DEVPORT
903 [4] = { "port", 0, &port_fops
, NULL
},
905 [5] = { "zero", 0666, &zero_fops
, &zero_bdi
},
906 [7] = { "full", 0666, &full_fops
, NULL
},
907 [8] = { "random", 0666, &random_fops
, NULL
},
908 [9] = { "urandom", 0666, &urandom_fops
, NULL
},
910 [11] = { "kmsg", 0644, &kmsg_fops
, NULL
},
912 #ifdef CONFIG_CRASH_DUMP
913 [12] = { "oldmem", 0, &oldmem_fops
, NULL
},
915 #ifdef CONFIG_KNOX_KAP
916 [13] = { "knox_kap", 0666, &knox_kap_fops
, NULL
},
918 #ifdef CONFIG_MST_LDO
919 [14] = { "mst_ctrl", 0666, &mst_ctrl_fops
, NULL
},
923 static int memory_open(struct inode
*inode
, struct file
*filp
)
926 const struct memdev
*dev
;
928 minor
= iminor(inode
);
929 if (minor
>= ARRAY_SIZE(devlist
))
932 dev
= &devlist
[minor
];
936 filp
->f_op
= dev
->fops
;
938 filp
->f_mapping
->backing_dev_info
= dev
->dev_info
;
940 /* Is /dev/mem or /dev/kmem ? */
941 if (dev
->dev_info
== &directly_mappable_cdev_bdi
)
942 filp
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
945 return dev
->fops
->open(inode
, filp
);
950 static const struct file_operations memory_fops
= {
952 .llseek
= noop_llseek
,
955 static char *mem_devnode(struct device
*dev
, umode_t
*mode
)
957 if (mode
&& devlist
[MINOR(dev
->devt
)].mode
)
958 *mode
= devlist
[MINOR(dev
->devt
)].mode
;
962 static struct class *mem_class
;
964 static int __init
chr_dev_init(void)
969 err
= bdi_init(&zero_bdi
);
973 if (register_chrdev(MEM_MAJOR
, "mem", &memory_fops
))
974 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
976 mem_class
= class_create(THIS_MODULE
, "mem");
977 if (IS_ERR(mem_class
))
978 return PTR_ERR(mem_class
);
980 mem_class
->devnode
= mem_devnode
;
981 for (minor
= 1; minor
< ARRAY_SIZE(devlist
); minor
++) {
982 if (!devlist
[minor
].name
)
988 if ((minor
== DEVPORT_MINOR
) && !arch_has_dev_port())
991 device_create(mem_class
, NULL
, MKDEV(MEM_MAJOR
, minor
),
992 NULL
, devlist
[minor
].name
);
998 fs_initcall(chr_dev_init
);