2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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/aio.h>
31 #include <asm/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)) {
71 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
72 current->comm, from, to);
81 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
87 void __weak unxlate_dev_mem_ptr(unsigned long 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;
102 if (!valid_phys_addr_range(p, count))
105 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
106 /* we don't have page 0 mapped on sparc and m68k.. */
108 sz = size_inside_page(p, count);
110 if (clear_user(buf, sz))
121 unsigned long remaining;
123 sz = size_inside_page(p, count);
125 if (!range_is_allowed(p >> PAGE_SHIFT, count))
129 * On ia64 if a page has been mapped somewhere as uncached, then
130 * it must also be accessed uncached by the kernel or data
131 * corruption may occur.
133 ptr = xlate_dev_mem_ptr(p);
137 remaining = copy_to_user(buf, ptr, sz);
138 unxlate_dev_mem_ptr(p, ptr);
152 static ssize_t write_mem(struct file *file, const char __user *buf,
153 size_t count, loff_t *ppos)
155 phys_addr_t p = *ppos;
157 unsigned long copied;
160 if (!valid_phys_addr_range(p, count))
165 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
166 /* we don't have page 0 mapped on sparc and m68k.. */
168 sz = size_inside_page(p, count);
169 /* Hmm. Do something? */
178 sz = size_inside_page(p, count);
180 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
184 * On ia64 if a page has been mapped somewhere as uncached, then
185 * it must also be accessed uncached by the kernel or data
186 * corruption may occur.
188 ptr = xlate_dev_mem_ptr(p);
195 copied = copy_from_user(ptr, buf, sz);
196 unxlate_dev_mem_ptr(p, ptr);
198 written += sz - copied;
214 int __weak phys_mem_access_prot_allowed(struct file *file,
215 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
220 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
223 * Architectures vary in how they handle caching for addresses
224 * outside of main memory.
227 #ifdef pgprot_noncached
228 static int uncached_access(struct file *file, phys_addr_t addr)
230 #if defined(CONFIG_IA64)
232 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
235 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
236 #elif defined(CONFIG_MIPS)
238 extern int __uncached_access(struct file *file,
241 return __uncached_access(file, addr);
245 * Accessing memory above the top the kernel knows about or through a
247 * that was marked O_DSYNC will be done non-cached.
249 if (file->f_flags & O_DSYNC)
251 return addr >= __pa(high_memory);
256 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
257 unsigned long size, pgprot_t vma_prot)
259 #ifdef pgprot_noncached
260 phys_addr_t offset = pfn << PAGE_SHIFT;
262 if (uncached_access(file, offset))
263 return pgprot_noncached(vma_prot);
270 static unsigned long get_unmapped_area_mem(struct file *file,
276 if (!valid_mmap_phys_addr_range(pgoff, len))
277 return (unsigned long) -EINVAL;
278 return pgoff << PAGE_SHIFT;
281 /* can't do an in-place private mapping if there's no MMU */
282 static inline int private_mapping_ok(struct vm_area_struct *vma)
284 return vma->vm_flags & VM_MAYSHARE;
287 #define get_unmapped_area_mem NULL
289 static inline int private_mapping_ok(struct vm_area_struct *vma)
295 static const struct vm_operations_struct mmap_mem_ops = {
296 #ifdef CONFIG_HAVE_IOREMAP_PROT
297 .access = generic_access_phys
301 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
303 size_t size = vma->vm_end - vma->vm_start;
305 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
308 if (!private_mapping_ok(vma))
311 if (!range_is_allowed(vma->vm_pgoff, size))
314 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
318 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
322 vma->vm_ops = &mmap_mem_ops;
324 /* Remap-pfn-range will mark the range VM_IO */
325 if (remap_pfn_range(vma,
329 vma->vm_page_prot)) {
335 #ifdef CONFIG_DEVKMEM
336 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
340 /* Turn a kernel-virtual address into a physical page frame */
341 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
344 * RED-PEN: on some architectures there is more mapped memory than
345 * available in mem_map which pfn_valid checks for. Perhaps should add a
348 * RED-PEN: vmalloc is not supported right now.
354 return mmap_mem(file, vma);
358 #ifdef CONFIG_DEVKMEM
360 * This function reads the *virtual* memory as seen by the kernel.
362 static ssize_t read_kmem(struct file *file, char __user *buf,
363 size_t count, loff_t *ppos)
365 unsigned long p = *ppos;
366 ssize_t low_count, read, sz;
367 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
371 if (p < (unsigned long) high_memory) {
373 if (count > (unsigned long)high_memory - p)
374 low_count = (unsigned long)high_memory - p;
376 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
377 /* we don't have page 0 mapped on sparc and m68k.. */
378 if (p < PAGE_SIZE && low_count > 0) {
379 sz = size_inside_page(p, low_count);
380 if (clear_user(buf, sz))
389 while (low_count > 0) {
390 sz = size_inside_page(p, low_count);
393 * On ia64 if a page has been mapped somewhere as
394 * uncached, then it must also be accessed uncached
395 * by the kernel or data corruption may occur
397 kbuf = xlate_dev_kmem_ptr((char *)p);
399 if (copy_to_user(buf, kbuf, sz))
410 kbuf = (char *)__get_free_page(GFP_KERNEL);
414 sz = size_inside_page(p, count);
415 if (!is_vmalloc_or_module_addr((void *)p)) {
419 sz = vread(kbuf, (char *)p, sz);
422 if (copy_to_user(buf, kbuf, sz)) {
431 free_page((unsigned long)kbuf);
434 return read ? read : err;
438 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
439 size_t count, loff_t *ppos)
442 unsigned long copied;
445 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
446 /* we don't have page 0 mapped on sparc and m68k.. */
448 sz = size_inside_page(p, count);
449 /* Hmm. Do something? */
460 sz = size_inside_page(p, count);
463 * On ia64 if a page has been mapped somewhere as uncached, then
464 * it must also be accessed uncached by the kernel or data
465 * corruption may occur.
467 ptr = xlate_dev_kmem_ptr((char *)p);
469 copied = copy_from_user(ptr, buf, sz);
471 written += sz - copied;
487 * This function writes to the *virtual* memory as seen by the kernel.
489 static ssize_t write_kmem(struct file *file, const char __user *buf,
490 size_t count, loff_t *ppos)
492 unsigned long p = *ppos;
495 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
498 if (p < (unsigned long) high_memory) {
499 unsigned long to_write = min_t(unsigned long, count,
500 (unsigned long)high_memory - p);
501 wrote = do_write_kmem(p, buf, to_write, ppos);
502 if (wrote != to_write)
510 kbuf = (char *)__get_free_page(GFP_KERNEL);
512 return wrote ? wrote : -ENOMEM;
514 unsigned long sz = size_inside_page(p, count);
517 if (!is_vmalloc_or_module_addr((void *)p)) {
521 n = copy_from_user(kbuf, buf, sz);
526 vwrite(kbuf, (char *)p, sz);
532 free_page((unsigned long)kbuf);
536 return virtr + wrote ? : err;
540 #ifdef CONFIG_DEVPORT
541 static ssize_t read_port(struct file *file, char __user *buf,
542 size_t count, loff_t *ppos)
544 unsigned long i = *ppos;
545 char __user *tmp = buf;
547 if (!access_ok(VERIFY_WRITE, buf, count))
549 while (count-- > 0 && i < 65536) {
550 if (__put_user(inb(i), tmp) < 0)
559 static ssize_t write_port(struct file *file, const char __user *buf,
560 size_t count, loff_t *ppos)
562 unsigned long i = *ppos;
563 const char __user *tmp = buf;
565 if (!access_ok(VERIFY_READ, buf, count))
567 while (count-- > 0 && i < 65536) {
569 if (__get_user(c, tmp)) {
583 static ssize_t read_null(struct file *file, char __user *buf,
584 size_t count, loff_t *ppos)
589 static ssize_t write_null(struct file *file, const char __user *buf,
590 size_t count, loff_t *ppos)
595 static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
596 unsigned long nr_segs, loff_t pos)
601 static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
602 unsigned long nr_segs, loff_t pos)
604 return iov_length(iov, nr_segs);
607 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
608 struct splice_desc *sd)
613 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
614 loff_t *ppos, size_t len, unsigned int flags)
616 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
619 static ssize_t read_zero(struct file *file, char __user *buf,
620 size_t count, loff_t *ppos)
627 if (!access_ok(VERIFY_WRITE, buf, count))
632 unsigned long unwritten;
633 size_t chunk = count;
635 if (chunk > PAGE_SIZE)
636 chunk = PAGE_SIZE; /* Just for latency reasons */
637 unwritten = __clear_user(buf, chunk);
638 written += chunk - unwritten;
641 if (signal_pending(current))
642 return written ? written : -ERESTARTSYS;
647 return written ? written : -EFAULT;
650 static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
651 unsigned long nr_segs, loff_t pos)
657 for (i = 0; i < nr_segs; i++) {
658 ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
665 return written ? written : -EFAULT;
668 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
673 if (vma->vm_flags & VM_SHARED)
674 return shmem_zero_setup(vma);
678 static ssize_t write_full(struct file *file, const char __user *buf,
679 size_t count, loff_t *ppos)
685 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
686 * can fopen() both devices with "a" now. This was previously impossible.
689 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
691 return file->f_pos = 0;
695 * The memory devices use the full 32/64 bits of the offset, and so we cannot
696 * check against negative addresses: they are ok. The return value is weird,
697 * though, in that case (0).
699 * also note that seeking relative to the "end of file" isn't supported:
700 * it has no meaning, so it returns -EINVAL.
702 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
706 mutex_lock(&file_inode(file)->i_mutex);
709 offset += file->f_pos;
711 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
712 if (IS_ERR_VALUE((unsigned long long)offset)) {
716 file->f_pos = offset;
718 force_successful_syscall_return();
723 mutex_unlock(&file_inode(file)->i_mutex);
727 static int open_port(struct inode *inode, struct file *filp)
729 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
732 #define zero_lseek null_lseek
733 #define full_lseek null_lseek
734 #define write_zero write_null
735 #define read_full read_zero
736 #define aio_write_zero aio_write_null
737 #define open_mem open_port
738 #define open_kmem open_mem
740 static const struct file_operations mem_fops = {
741 .llseek = memory_lseek,
746 .get_unmapped_area = get_unmapped_area_mem,
749 #ifdef CONFIG_DEVKMEM
750 static const struct file_operations kmem_fops = {
751 .llseek = memory_lseek,
756 .get_unmapped_area = get_unmapped_area_mem,
760 static const struct file_operations null_fops = {
761 .llseek = null_lseek,
764 .aio_read = aio_read_null,
765 .aio_write = aio_write_null,
766 .splice_write = splice_write_null,
769 #ifdef CONFIG_DEVPORT
770 static const struct file_operations port_fops = {
771 .llseek = memory_lseek,
778 static const struct file_operations zero_fops = {
779 .llseek = zero_lseek,
782 .aio_read = aio_read_zero,
783 .aio_write = aio_write_zero,
788 * capabilities for /dev/zero
789 * - permits private mappings, "copies" are taken of the source of zeros
790 * - no writeback happens
792 static struct backing_dev_info zero_bdi = {
794 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
797 static const struct file_operations full_fops = {
798 .llseek = full_lseek,
803 static const struct memdev {
806 const struct file_operations *fops;
807 struct backing_dev_info *dev_info;
809 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
810 #ifdef CONFIG_DEVKMEM
811 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
813 [3] = { "null", 0666, &null_fops, NULL },
814 #ifdef CONFIG_DEVPORT
815 [4] = { "port", 0, &port_fops, NULL },
817 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
818 [7] = { "full", 0666, &full_fops, NULL },
819 [8] = { "random", 0666, &random_fops, NULL },
820 [9] = { "urandom", 0666, &urandom_fops, NULL },
822 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
826 static int memory_open(struct inode *inode, struct file *filp)
829 const struct memdev *dev;
831 minor = iminor(inode);
832 if (minor >= ARRAY_SIZE(devlist))
835 dev = &devlist[minor];
839 filp->f_op = dev->fops;
841 filp->f_mapping->backing_dev_info = dev->dev_info;
843 /* Is /dev/mem or /dev/kmem ? */
844 if (dev->dev_info == &directly_mappable_cdev_bdi)
845 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
848 return dev->fops->open(inode, filp);
853 static const struct file_operations memory_fops = {
855 .llseek = noop_llseek,
858 static char *mem_devnode(struct device *dev, umode_t *mode)
860 if (mode && devlist[MINOR(dev->devt)].mode)
861 *mode = devlist[MINOR(dev->devt)].mode;
865 static struct class *mem_class;
867 static int __init chr_dev_init(void)
872 err = bdi_init(&zero_bdi);
876 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
877 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
879 mem_class = class_create(THIS_MODULE, "mem");
880 if (IS_ERR(mem_class))
881 return PTR_ERR(mem_class);
883 mem_class->devnode = mem_devnode;
884 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
885 if (!devlist[minor].name)
891 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
894 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
895 NULL, devlist[minor].name);
901 fs_initcall(chr_dev_init);