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/shmem_fs.h>
26 #include <linux/splice.h>
27 #include <linux/pfn.h>
28 #include <linux/export.h>
30 #include <linux/uio.h>
32 #include <linux/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))
78 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
84 #ifndef unxlate_dev_mem_ptr
85 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
86 void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
92 * This funcion reads the *physical* memory. The f_pos points directly to the
95 static ssize_t read_mem(struct file *file, char __user *buf,
96 size_t count, loff_t *ppos)
98 phys_addr_t p = *ppos;
105 if (!valid_phys_addr_range(p, count))
108 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
109 /* we don't have page 0 mapped on sparc and m68k.. */
111 sz = size_inside_page(p, count);
113 if (clear_user(buf, sz))
124 unsigned long remaining;
126 sz = size_inside_page(p, count);
128 if (!range_is_allowed(p >> PAGE_SHIFT, count))
132 * On ia64 if a page has been mapped somewhere as uncached, then
133 * it must also be accessed uncached by the kernel or data
134 * corruption may occur.
136 ptr = xlate_dev_mem_ptr(p);
140 remaining = copy_to_user(buf, ptr, sz);
141 unxlate_dev_mem_ptr(p, ptr);
155 static ssize_t write_mem(struct file *file, const char __user *buf,
156 size_t count, loff_t *ppos)
158 phys_addr_t p = *ppos;
160 unsigned long copied;
166 if (!valid_phys_addr_range(p, count))
171 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
172 /* we don't have page 0 mapped on sparc and m68k.. */
174 sz = size_inside_page(p, count);
175 /* Hmm. Do something? */
184 sz = size_inside_page(p, count);
186 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
190 * On ia64 if a page has been mapped somewhere as uncached, then
191 * it must also be accessed uncached by the kernel or data
192 * corruption may occur.
194 ptr = xlate_dev_mem_ptr(p);
201 copied = copy_from_user(ptr, buf, sz);
202 unxlate_dev_mem_ptr(p, ptr);
204 written += sz - copied;
220 int __weak phys_mem_access_prot_allowed(struct file *file,
221 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
226 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
229 * Architectures vary in how they handle caching for addresses
230 * outside of main memory.
233 #ifdef pgprot_noncached
234 static int uncached_access(struct file *file, phys_addr_t addr)
236 #if defined(CONFIG_IA64)
238 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
241 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
242 #elif defined(CONFIG_MIPS)
244 extern int __uncached_access(struct file *file,
247 return __uncached_access(file, addr);
251 * Accessing memory above the top the kernel knows about or through a
253 * that was marked O_DSYNC will be done non-cached.
255 if (file->f_flags & O_DSYNC)
257 return addr >= __pa(high_memory);
262 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
263 unsigned long size, pgprot_t vma_prot)
265 #ifdef pgprot_noncached
266 phys_addr_t offset = pfn << PAGE_SHIFT;
268 if (uncached_access(file, offset))
269 return pgprot_noncached(vma_prot);
276 static unsigned long get_unmapped_area_mem(struct file *file,
282 if (!valid_mmap_phys_addr_range(pgoff, len))
283 return (unsigned long) -EINVAL;
284 return pgoff << PAGE_SHIFT;
287 /* permit direct mmap, for read, write or exec */
288 static unsigned memory_mmap_capabilities(struct file *file)
290 return NOMMU_MAP_DIRECT |
291 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
294 static unsigned zero_mmap_capabilities(struct file *file)
296 return NOMMU_MAP_COPY;
299 /* can't do an in-place private mapping if there's no MMU */
300 static inline int private_mapping_ok(struct vm_area_struct *vma)
302 return vma->vm_flags & VM_MAYSHARE;
306 static inline int private_mapping_ok(struct vm_area_struct *vma)
312 static const struct vm_operations_struct mmap_mem_ops = {
313 #ifdef CONFIG_HAVE_IOREMAP_PROT
314 .access = generic_access_phys
318 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
320 size_t size = vma->vm_end - vma->vm_start;
322 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
325 if (!private_mapping_ok(vma))
328 if (!range_is_allowed(vma->vm_pgoff, size))
331 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
335 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
339 vma->vm_ops = &mmap_mem_ops;
341 /* Remap-pfn-range will mark the range VM_IO */
342 if (remap_pfn_range(vma,
346 vma->vm_page_prot)) {
352 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
356 /* Turn a kernel-virtual address into a physical page frame */
357 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
360 * RED-PEN: on some architectures there is more mapped memory than
361 * available in mem_map which pfn_valid checks for. Perhaps should add a
364 * RED-PEN: vmalloc is not supported right now.
370 return mmap_mem(file, vma);
374 * This function reads the *virtual* memory as seen by the kernel.
376 static ssize_t read_kmem(struct file *file, char __user *buf,
377 size_t count, loff_t *ppos)
379 unsigned long p = *ppos;
380 ssize_t low_count, read, sz;
381 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
384 if (!pfn_valid(PFN_DOWN(p)))
388 if (p < (unsigned long) high_memory) {
390 if (count > (unsigned long)high_memory - p)
391 low_count = (unsigned long)high_memory - p;
393 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
394 /* we don't have page 0 mapped on sparc and m68k.. */
395 if (p < PAGE_SIZE && low_count > 0) {
396 sz = size_inside_page(p, low_count);
397 if (clear_user(buf, sz))
406 while (low_count > 0) {
407 sz = size_inside_page(p, low_count);
410 * On ia64 if a page has been mapped somewhere as
411 * uncached, then it must also be accessed uncached
412 * by the kernel or data corruption may occur
414 kbuf = xlate_dev_kmem_ptr((void *)p);
416 if (copy_to_user(buf, kbuf, sz))
427 kbuf = (char *)__get_free_page(GFP_KERNEL);
431 sz = size_inside_page(p, count);
432 if (!is_vmalloc_or_module_addr((void *)p)) {
436 sz = vread(kbuf, (char *)p, sz);
439 if (copy_to_user(buf, kbuf, sz)) {
448 free_page((unsigned long)kbuf);
451 return read ? read : err;
455 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
456 size_t count, loff_t *ppos)
459 unsigned long copied;
462 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
463 /* we don't have page 0 mapped on sparc and m68k.. */
465 sz = size_inside_page(p, count);
466 /* Hmm. Do something? */
477 sz = size_inside_page(p, count);
480 * On ia64 if a page has been mapped somewhere as uncached, then
481 * it must also be accessed uncached by the kernel or data
482 * corruption may occur.
484 ptr = xlate_dev_kmem_ptr((void *)p);
486 copied = copy_from_user(ptr, buf, sz);
488 written += sz - copied;
504 * This function writes to the *virtual* memory as seen by the kernel.
506 static ssize_t write_kmem(struct file *file, const char __user *buf,
507 size_t count, loff_t *ppos)
509 unsigned long p = *ppos;
512 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
515 if (!pfn_valid(PFN_DOWN(p)))
518 if (p < (unsigned long) high_memory) {
519 unsigned long to_write = min_t(unsigned long, count,
520 (unsigned long)high_memory - p);
521 wrote = do_write_kmem(p, buf, to_write, ppos);
522 if (wrote != to_write)
530 kbuf = (char *)__get_free_page(GFP_KERNEL);
532 return wrote ? wrote : -ENOMEM;
534 unsigned long sz = size_inside_page(p, count);
537 if (!is_vmalloc_or_module_addr((void *)p)) {
541 n = copy_from_user(kbuf, buf, sz);
546 vwrite(kbuf, (char *)p, sz);
552 free_page((unsigned long)kbuf);
556 return virtr + wrote ? : err;
559 static ssize_t read_port(struct file *file, char __user *buf,
560 size_t count, loff_t *ppos)
562 unsigned long i = *ppos;
563 char __user *tmp = buf;
565 if (!access_ok(VERIFY_WRITE, buf, count))
567 while (count-- > 0 && i < 65536) {
568 if (__put_user(inb(i), tmp) < 0)
577 static ssize_t write_port(struct file *file, const char __user *buf,
578 size_t count, loff_t *ppos)
580 unsigned long i = *ppos;
581 const char __user *tmp = buf;
583 if (!access_ok(VERIFY_READ, buf, count))
585 while (count-- > 0 && i < 65536) {
588 if (__get_user(c, tmp)) {
601 static ssize_t read_null(struct file *file, char __user *buf,
602 size_t count, loff_t *ppos)
607 static ssize_t write_null(struct file *file, const char __user *buf,
608 size_t count, loff_t *ppos)
613 static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
618 static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
620 size_t count = iov_iter_count(from);
621 iov_iter_advance(from, count);
625 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
626 struct splice_desc *sd)
631 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
632 loff_t *ppos, size_t len, unsigned int flags)
634 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
637 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
641 while (iov_iter_count(iter)) {
642 size_t chunk = iov_iter_count(iter), n;
644 if (chunk > PAGE_SIZE)
645 chunk = PAGE_SIZE; /* Just for latency reasons */
646 n = iov_iter_zero(chunk, iter);
647 if (!n && iov_iter_count(iter))
648 return written ? written : -EFAULT;
650 if (signal_pending(current))
651 return written ? written : -ERESTARTSYS;
657 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
662 if (vma->vm_flags & VM_SHARED)
663 return shmem_zero_setup(vma);
667 static unsigned long get_unmapped_area_zero(struct file *file,
668 unsigned long addr, unsigned long len,
669 unsigned long pgoff, unsigned long flags)
672 if (flags & MAP_SHARED) {
674 * mmap_zero() will call shmem_zero_setup() to create a file,
675 * so use shmem's get_unmapped_area in case it can be huge;
676 * and pass NULL for file as in mmap.c's get_unmapped_area(),
677 * so as not to confuse shmem with our handle on "/dev/zero".
679 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
682 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
683 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
689 static ssize_t write_full(struct file *file, const char __user *buf,
690 size_t count, loff_t *ppos)
696 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
697 * can fopen() both devices with "a" now. This was previously impossible.
700 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
702 return file->f_pos = 0;
706 * The memory devices use the full 32/64 bits of the offset, and so we cannot
707 * check against negative addresses: they are ok. The return value is weird,
708 * though, in that case (0).
710 * also note that seeking relative to the "end of file" isn't supported:
711 * it has no meaning, so it returns -EINVAL.
713 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
717 inode_lock(file_inode(file));
720 offset += file->f_pos;
722 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
723 if ((unsigned long long)offset >= -MAX_ERRNO) {
727 file->f_pos = offset;
729 force_successful_syscall_return();
734 inode_unlock(file_inode(file));
738 static int open_port(struct inode *inode, struct file *filp)
740 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
743 #define zero_lseek null_lseek
744 #define full_lseek null_lseek
745 #define write_zero write_null
746 #define write_iter_zero write_iter_null
747 #define open_mem open_port
748 #define open_kmem open_mem
750 static const struct file_operations __maybe_unused mem_fops = {
751 .llseek = memory_lseek,
757 .get_unmapped_area = get_unmapped_area_mem,
758 .mmap_capabilities = memory_mmap_capabilities,
762 static const struct file_operations __maybe_unused kmem_fops = {
763 .llseek = memory_lseek,
769 .get_unmapped_area = get_unmapped_area_mem,
770 .mmap_capabilities = memory_mmap_capabilities,
774 static const struct file_operations null_fops = {
775 .llseek = null_lseek,
778 .read_iter = read_iter_null,
779 .write_iter = write_iter_null,
780 .splice_write = splice_write_null,
783 static const struct file_operations __maybe_unused port_fops = {
784 .llseek = memory_lseek,
790 static const struct file_operations zero_fops = {
791 .llseek = zero_lseek,
793 .read_iter = read_iter_zero,
794 .write_iter = write_iter_zero,
796 .get_unmapped_area = get_unmapped_area_zero,
798 .mmap_capabilities = zero_mmap_capabilities,
802 static const struct file_operations full_fops = {
803 .llseek = full_lseek,
804 .read_iter = read_iter_zero,
808 static const struct memdev {
811 const struct file_operations *fops;
815 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
817 #ifdef CONFIG_DEVKMEM
818 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
820 [3] = { "null", 0666, &null_fops, 0 },
821 #ifdef CONFIG_DEVPORT
822 [4] = { "port", 0, &port_fops, 0 },
824 [5] = { "zero", 0666, &zero_fops, 0 },
825 [7] = { "full", 0666, &full_fops, 0 },
826 [8] = { "random", 0666, &random_fops, 0 },
827 [9] = { "urandom", 0666, &urandom_fops, 0 },
829 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
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;
847 filp->f_mode |= dev->fmode;
850 return dev->fops->open(inode, filp);
855 static const struct file_operations memory_fops = {
857 .llseek = noop_llseek,
860 static char *mem_devnode(struct device *dev, umode_t *mode)
862 if (mode && devlist[MINOR(dev->devt)].mode)
863 *mode = devlist[MINOR(dev->devt)].mode;
867 static struct class *mem_class;
869 static int __init chr_dev_init(void)
873 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
874 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
876 mem_class = class_create(THIS_MODULE, "mem");
877 if (IS_ERR(mem_class))
878 return PTR_ERR(mem_class);
880 mem_class->devnode = mem_devnode;
881 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
882 if (!devlist[minor].name)
888 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
891 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
892 NULL, devlist[minor].name);
898 fs_initcall(chr_dev_init);