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/splice.h>
26 #include <linux/pfn.h>
27 #include <linux/export.h>
29 #include <linux/uio.h>
31 #include <linux/uaccess.h>
34 # include <linux/efi.h>
37 #define DEVPORT_MINOR 4
39 static inline unsigned long size_inside_page(unsigned long start,
44 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
49 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
50 static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
52 return addr + count <= __pa(high_memory);
55 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
61 #ifdef CONFIG_STRICT_DEVMEM
62 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
64 u64 from = ((u64)pfn) << PAGE_SHIFT;
69 if (!devmem_is_allowed(pfn))
77 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
83 #ifndef unxlate_dev_mem_ptr
84 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
85 void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
91 * This funcion reads the *physical* memory. The f_pos points directly to the
94 static ssize_t read_mem(struct file *file, char __user *buf,
95 size_t count, loff_t *ppos)
97 phys_addr_t p = *ppos;
104 if (!valid_phys_addr_range(p, count))
107 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
108 /* we don't have page 0 mapped on sparc and m68k.. */
110 sz = size_inside_page(p, count);
112 if (clear_user(buf, sz))
123 unsigned long remaining;
125 sz = size_inside_page(p, count);
127 if (!range_is_allowed(p >> PAGE_SHIFT, count))
131 * On ia64 if a page has been mapped somewhere as uncached, then
132 * it must also be accessed uncached by the kernel or data
133 * corruption may occur.
135 ptr = xlate_dev_mem_ptr(p);
139 remaining = copy_to_user(buf, ptr, sz);
140 unxlate_dev_mem_ptr(p, ptr);
154 static ssize_t write_mem(struct file *file, const char __user *buf,
155 size_t count, loff_t *ppos)
157 phys_addr_t p = *ppos;
159 unsigned long copied;
165 if (!valid_phys_addr_range(p, count))
170 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
171 /* we don't have page 0 mapped on sparc and m68k.. */
173 sz = size_inside_page(p, count);
174 /* Hmm. Do something? */
183 sz = size_inside_page(p, count);
185 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
189 * On ia64 if a page has been mapped somewhere as uncached, then
190 * it must also be accessed uncached by the kernel or data
191 * corruption may occur.
193 ptr = xlate_dev_mem_ptr(p);
200 copied = copy_from_user(ptr, buf, sz);
201 unxlate_dev_mem_ptr(p, ptr);
203 written += sz - copied;
219 int __weak phys_mem_access_prot_allowed(struct file *file,
220 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
225 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
228 * Architectures vary in how they handle caching for addresses
229 * outside of main memory.
232 #ifdef pgprot_noncached
233 static int uncached_access(struct file *file, phys_addr_t addr)
235 #if defined(CONFIG_IA64)
237 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
240 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
241 #elif defined(CONFIG_MIPS)
243 extern int __uncached_access(struct file *file,
246 return __uncached_access(file, addr);
250 * Accessing memory above the top the kernel knows about or through a
252 * that was marked O_DSYNC will be done non-cached.
254 if (file->f_flags & O_DSYNC)
256 return addr >= __pa(high_memory);
261 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
262 unsigned long size, pgprot_t vma_prot)
264 #ifdef pgprot_noncached
265 phys_addr_t offset = pfn << PAGE_SHIFT;
267 if (uncached_access(file, offset))
268 return pgprot_noncached(vma_prot);
275 static unsigned long get_unmapped_area_mem(struct file *file,
281 if (!valid_mmap_phys_addr_range(pgoff, len))
282 return (unsigned long) -EINVAL;
283 return pgoff << PAGE_SHIFT;
286 /* permit direct mmap, for read, write or exec */
287 static unsigned memory_mmap_capabilities(struct file *file)
289 return NOMMU_MAP_DIRECT |
290 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
293 static unsigned zero_mmap_capabilities(struct file *file)
295 return NOMMU_MAP_COPY;
298 /* can't do an in-place private mapping if there's no MMU */
299 static inline int private_mapping_ok(struct vm_area_struct *vma)
301 return vma->vm_flags & VM_MAYSHARE;
305 static inline int private_mapping_ok(struct vm_area_struct *vma)
311 static const struct vm_operations_struct mmap_mem_ops = {
312 #ifdef CONFIG_HAVE_IOREMAP_PROT
313 .access = generic_access_phys
317 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
319 size_t size = vma->vm_end - vma->vm_start;
321 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
324 if (!private_mapping_ok(vma))
327 if (!range_is_allowed(vma->vm_pgoff, size))
330 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
334 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
338 vma->vm_ops = &mmap_mem_ops;
340 /* Remap-pfn-range will mark the range VM_IO */
341 if (remap_pfn_range(vma,
345 vma->vm_page_prot)) {
351 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
355 /* Turn a kernel-virtual address into a physical page frame */
356 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
359 * RED-PEN: on some architectures there is more mapped memory than
360 * available in mem_map which pfn_valid checks for. Perhaps should add a
363 * RED-PEN: vmalloc is not supported right now.
369 return mmap_mem(file, vma);
373 * This function reads the *virtual* memory as seen by the kernel.
375 static ssize_t read_kmem(struct file *file, char __user *buf,
376 size_t count, loff_t *ppos)
378 unsigned long p = *ppos;
379 ssize_t low_count, read, sz;
380 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
384 if (p < (unsigned long) high_memory) {
386 if (count > (unsigned long)high_memory - p)
387 low_count = (unsigned long)high_memory - p;
389 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
390 /* we don't have page 0 mapped on sparc and m68k.. */
391 if (p < PAGE_SIZE && low_count > 0) {
392 sz = size_inside_page(p, low_count);
393 if (clear_user(buf, sz))
402 while (low_count > 0) {
403 sz = size_inside_page(p, low_count);
406 * On ia64 if a page has been mapped somewhere as
407 * uncached, then it must also be accessed uncached
408 * by the kernel or data corruption may occur
410 kbuf = xlate_dev_kmem_ptr((void *)p);
412 if (copy_to_user(buf, kbuf, sz))
423 kbuf = (char *)__get_free_page(GFP_KERNEL);
427 sz = size_inside_page(p, count);
428 if (!is_vmalloc_or_module_addr((void *)p)) {
432 sz = vread(kbuf, (char *)p, sz);
435 if (copy_to_user(buf, kbuf, sz)) {
444 free_page((unsigned long)kbuf);
447 return read ? read : err;
451 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
452 size_t count, loff_t *ppos)
455 unsigned long copied;
458 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
459 /* we don't have page 0 mapped on sparc and m68k.. */
461 sz = size_inside_page(p, count);
462 /* Hmm. Do something? */
473 sz = size_inside_page(p, count);
476 * On ia64 if a page has been mapped somewhere as uncached, then
477 * it must also be accessed uncached by the kernel or data
478 * corruption may occur.
480 ptr = xlate_dev_kmem_ptr((void *)p);
482 copied = copy_from_user(ptr, buf, sz);
484 written += sz - copied;
500 * This function writes to the *virtual* memory as seen by the kernel.
502 static ssize_t write_kmem(struct file *file, const char __user *buf,
503 size_t count, loff_t *ppos)
505 unsigned long p = *ppos;
508 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
511 if (p < (unsigned long) high_memory) {
512 unsigned long to_write = min_t(unsigned long, count,
513 (unsigned long)high_memory - p);
514 wrote = do_write_kmem(p, buf, to_write, ppos);
515 if (wrote != to_write)
523 kbuf = (char *)__get_free_page(GFP_KERNEL);
525 return wrote ? wrote : -ENOMEM;
527 unsigned long sz = size_inside_page(p, count);
530 if (!is_vmalloc_or_module_addr((void *)p)) {
534 n = copy_from_user(kbuf, buf, sz);
539 vwrite(kbuf, (char *)p, sz);
545 free_page((unsigned long)kbuf);
549 return virtr + wrote ? : err;
552 static ssize_t read_port(struct file *file, char __user *buf,
553 size_t count, loff_t *ppos)
555 unsigned long i = *ppos;
556 char __user *tmp = buf;
558 if (!access_ok(VERIFY_WRITE, buf, count))
560 while (count-- > 0 && i < 65536) {
561 if (__put_user(inb(i), tmp) < 0)
570 static ssize_t write_port(struct file *file, const char __user *buf,
571 size_t count, loff_t *ppos)
573 unsigned long i = *ppos;
574 const char __user *tmp = buf;
576 if (!access_ok(VERIFY_READ, buf, count))
578 while (count-- > 0 && i < 65536) {
581 if (__get_user(c, tmp)) {
594 static ssize_t read_null(struct file *file, char __user *buf,
595 size_t count, loff_t *ppos)
600 static ssize_t write_null(struct file *file, const char __user *buf,
601 size_t count, loff_t *ppos)
606 static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
611 static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
613 size_t count = iov_iter_count(from);
614 iov_iter_advance(from, count);
618 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
619 struct splice_desc *sd)
624 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
625 loff_t *ppos, size_t len, unsigned int flags)
627 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
630 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
634 while (iov_iter_count(iter)) {
635 size_t chunk = iov_iter_count(iter), n;
637 if (chunk > PAGE_SIZE)
638 chunk = PAGE_SIZE; /* Just for latency reasons */
639 n = iov_iter_zero(chunk, iter);
640 if (!n && iov_iter_count(iter))
641 return written ? written : -EFAULT;
643 if (signal_pending(current))
644 return written ? written : -ERESTARTSYS;
650 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
655 if (vma->vm_flags & VM_SHARED)
656 return shmem_zero_setup(vma);
660 static ssize_t write_full(struct file *file, const char __user *buf,
661 size_t count, loff_t *ppos)
667 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
668 * can fopen() both devices with "a" now. This was previously impossible.
671 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
673 return file->f_pos = 0;
677 * The memory devices use the full 32/64 bits of the offset, and so we cannot
678 * check against negative addresses: they are ok. The return value is weird,
679 * though, in that case (0).
681 * also note that seeking relative to the "end of file" isn't supported:
682 * it has no meaning, so it returns -EINVAL.
684 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
688 inode_lock(file_inode(file));
691 offset += file->f_pos;
693 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
694 if ((unsigned long long)offset >= -MAX_ERRNO) {
698 file->f_pos = offset;
700 force_successful_syscall_return();
705 inode_unlock(file_inode(file));
709 static int open_port(struct inode *inode, struct file *filp)
711 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
714 #define zero_lseek null_lseek
715 #define full_lseek null_lseek
716 #define write_zero write_null
717 #define write_iter_zero write_iter_null
718 #define open_mem open_port
719 #define open_kmem open_mem
721 static const struct file_operations __maybe_unused mem_fops = {
722 .llseek = memory_lseek,
728 .get_unmapped_area = get_unmapped_area_mem,
729 .mmap_capabilities = memory_mmap_capabilities,
733 static const struct file_operations __maybe_unused kmem_fops = {
734 .llseek = memory_lseek,
740 .get_unmapped_area = get_unmapped_area_mem,
741 .mmap_capabilities = memory_mmap_capabilities,
745 static const struct file_operations null_fops = {
746 .llseek = null_lseek,
749 .read_iter = read_iter_null,
750 .write_iter = write_iter_null,
751 .splice_write = splice_write_null,
754 static const struct file_operations __maybe_unused port_fops = {
755 .llseek = memory_lseek,
761 static const struct file_operations zero_fops = {
762 .llseek = zero_lseek,
764 .read_iter = read_iter_zero,
765 .write_iter = write_iter_zero,
768 .mmap_capabilities = zero_mmap_capabilities,
772 static const struct file_operations full_fops = {
773 .llseek = full_lseek,
774 .read_iter = read_iter_zero,
778 static const struct memdev {
781 const struct file_operations *fops;
785 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
787 #ifdef CONFIG_DEVKMEM
788 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
790 [3] = { "null", 0666, &null_fops, 0 },
791 #ifdef CONFIG_DEVPORT
792 [4] = { "port", 0, &port_fops, 0 },
794 [5] = { "zero", 0666, &zero_fops, 0 },
795 [7] = { "full", 0666, &full_fops, 0 },
796 [8] = { "random", 0666, &random_fops, 0 },
797 [9] = { "urandom", 0666, &urandom_fops, 0 },
799 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
803 static int memory_open(struct inode *inode, struct file *filp)
806 const struct memdev *dev;
808 minor = iminor(inode);
809 if (minor >= ARRAY_SIZE(devlist))
812 dev = &devlist[minor];
816 filp->f_op = dev->fops;
817 filp->f_mode |= dev->fmode;
820 return dev->fops->open(inode, filp);
825 static const struct file_operations memory_fops = {
827 .llseek = noop_llseek,
830 static char *mem_devnode(struct device *dev, umode_t *mode)
832 if (mode && devlist[MINOR(dev->devt)].mode)
833 *mode = devlist[MINOR(dev->devt)].mode;
837 static struct class *mem_class;
839 static int __init chr_dev_init(void)
843 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
844 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
846 mem_class = class_create(THIS_MODULE, "mem");
847 if (IS_ERR(mem_class))
848 return PTR_ERR(mem_class);
850 mem_class->devnode = mem_devnode;
851 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
852 if (!devlist[minor].name)
858 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
861 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
862 NULL, devlist[minor].name);
868 fs_initcall(chr_dev_init);