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 mmaping 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/smp_lock.h>
22 #include <linux/ptrace.h>
23 #include <linux/device.h>
24 #include <linux/highmem.h>
25 #include <linux/crash_dump.h>
26 #include <linux/backing-dev.h>
27 #include <linux/bootmem.h>
28 #include <linux/pipe_fs_i.h>
30 #include <asm/uaccess.h>
34 # include <linux/efi.h>
37 static inline int range_is_allowed(unsigned long from, unsigned long to)
41 cursor = from >> PAGE_SHIFT;
42 while ((cursor << PAGE_SHIFT) < to) {
43 if (!devmem_is_allowed(cursor)) {
44 printk ("Program %s tried to read /dev/mem between %lx->%lx.\n",
45 current->comm, from, to);
54 * Architectures vary in how they handle caching for addresses
55 * outside of main memory.
58 static inline int uncached_access(struct file *file, unsigned long addr)
62 * On the PPro and successors, the MTRRs are used to set
63 * memory types for physical addresses outside main memory,
64 * so blindly setting PCD or PWT on those pages is wrong.
65 * For Pentiums and earlier, the surround logic should disable
66 * caching for the high addresses through the KEN pin, but
67 * we maintain the tradition of paranoia in this code.
69 if (file->f_flags & O_SYNC)
71 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
72 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
73 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
74 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
75 && addr >= __pa(high_memory);
76 #elif defined(__x86_64__)
78 * This is broken because it can generate memory type aliases,
79 * which can cause cache corruptions
80 * But it is only available for root and we have to be bug-to-bug
81 * compatible with i386.
83 if (file->f_flags & O_SYNC)
85 /* same behaviour as i386. PAT always set to cached and MTRRs control the
87 Hopefully a full PAT implementation will fix that soon. */
89 #elif defined(CONFIG_IA64)
91 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
93 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
96 * Accessing memory above the top the kernel knows about or through a file pointer
97 * that was marked O_SYNC will be done non-cached.
99 if (file->f_flags & O_SYNC)
101 return addr >= __pa(high_memory);
105 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
106 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
108 if (addr + count > __pa(high_memory))
114 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
120 #ifndef ARCH_HAS_DEV_MEM
122 * This funcion reads the *physical* memory. The f_pos points directly to the
125 static ssize_t read_mem(struct file * file, char __user * buf,
126 size_t count, loff_t *ppos)
128 unsigned long p = *ppos;
132 if (!valid_phys_addr_range(p, count))
135 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
136 /* we don't have page 0 mapped on sparc and m68k.. */
142 if (clear_user(buf, sz))
154 * Handle first page in case it's not aligned
156 if (-p & (PAGE_SIZE - 1))
157 sz = -p & (PAGE_SIZE - 1);
161 sz = min_t(unsigned long, sz, count);
164 * On ia64 if a page has been mapped somewhere as
165 * uncached, then it must also be accessed uncached
166 * by the kernel or data corruption may occur
168 ptr = xlate_dev_mem_ptr(p);
170 if (!range_is_allowed(p, p+count))
172 if (copy_to_user(buf, ptr, sz))
184 static ssize_t write_mem(struct file * file, const char __user * buf,
185 size_t count, loff_t *ppos)
187 unsigned long p = *ppos;
189 unsigned long copied;
192 if (!valid_phys_addr_range(p, count))
197 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
198 /* we don't have page 0 mapped on sparc and m68k.. */
200 unsigned long sz = PAGE_SIZE - p;
203 /* Hmm. Do something? */
213 * Handle first page in case it's not aligned
215 if (-p & (PAGE_SIZE - 1))
216 sz = -p & (PAGE_SIZE - 1);
220 sz = min_t(unsigned long, sz, count);
223 * On ia64 if a page has been mapped somewhere as
224 * uncached, then it must also be accessed uncached
225 * by the kernel or data corruption may occur
227 ptr = xlate_dev_mem_ptr(p);
229 if (!range_is_allowed(ptr, ptr+sz))
231 copied = copy_from_user(ptr, buf, sz);
233 written += sz - copied;
249 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
250 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
251 unsigned long size, pgprot_t vma_prot)
253 #ifdef pgprot_noncached
254 unsigned long offset = pfn << PAGE_SHIFT;
256 if (uncached_access(file, offset))
257 return pgprot_noncached(vma_prot);
263 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
265 size_t size = vma->vm_end - vma->vm_start;
267 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
270 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
274 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
275 if (remap_pfn_range(vma,
284 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
288 /* Turn a kernel-virtual address into a physical page frame */
289 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
292 * RED-PEN: on some architectures there is more mapped memory
293 * than available in mem_map which pfn_valid checks
294 * for. Perhaps should add a new macro here.
296 * RED-PEN: vmalloc is not supported right now.
302 return mmap_mem(file, vma);
305 #ifdef CONFIG_CRASH_DUMP
307 * Read memory corresponding to the old kernel.
309 static ssize_t read_oldmem(struct file *file, char __user *buf,
310 size_t count, loff_t *ppos)
312 unsigned long pfn, offset;
313 size_t read = 0, csize;
317 pfn = *ppos / PAGE_SIZE;
318 if (pfn > saved_max_pfn)
321 offset = (unsigned long)(*ppos % PAGE_SIZE);
322 if (count > PAGE_SIZE - offset)
323 csize = PAGE_SIZE - offset;
327 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
339 extern long vread(char *buf, char *addr, unsigned long count);
340 extern long vwrite(char *buf, char *addr, unsigned long count);
343 * This function reads the *virtual* memory as seen by the kernel.
345 static ssize_t read_kmem(struct file *file, char __user *buf,
346 size_t count, loff_t *ppos)
348 unsigned long p = *ppos;
349 ssize_t low_count, read, sz;
350 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
355 if (p < (unsigned long) high_memory) {
357 if (count > (unsigned long) high_memory - p)
358 low_count = (unsigned long) high_memory - p;
360 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
361 /* we don't have page 0 mapped on sparc and m68k.. */
362 if (p < PAGE_SIZE && low_count > 0) {
363 size_t tmp = PAGE_SIZE - p;
364 if (tmp > low_count) tmp = low_count;
365 if (clear_user(buf, tmp))
374 while (low_count > 0) {
376 * Handle first page in case it's not aligned
378 if (-p & (PAGE_SIZE - 1))
379 sz = -p & (PAGE_SIZE - 1);
383 sz = min_t(unsigned long, sz, low_count);
386 * On ia64 if a page has been mapped somewhere as
387 * uncached, then it must also be accessed uncached
388 * by the kernel or data corruption may occur
390 kbuf = xlate_dev_kmem_ptr((char *)p);
392 if (copy_to_user(buf, kbuf, sz))
403 kbuf = (char *)__get_free_page(GFP_KERNEL);
411 len = vread(kbuf, (char *)p, len);
414 if (copy_to_user(buf, kbuf, len)) {
415 free_page((unsigned long)kbuf);
423 free_page((unsigned long)kbuf);
430 #if defined(CONFIG_ISA) || !defined(__mc68000__)
431 static ssize_t read_port(struct file * file, char __user * buf,
432 size_t count, loff_t *ppos)
434 unsigned long i = *ppos;
435 char __user *tmp = buf;
437 if (!access_ok(VERIFY_WRITE, buf, count))
439 while (count-- > 0 && i < 65536) {
440 if (__put_user(inb(i),tmp) < 0)
449 static ssize_t write_port(struct file * file, const char __user * buf,
450 size_t count, loff_t *ppos)
452 unsigned long i = *ppos;
453 const char __user * tmp = buf;
455 if (!access_ok(VERIFY_READ,buf,count))
457 while (count-- > 0 && i < 65536) {
459 if (__get_user(c, tmp)) {
473 static ssize_t read_null(struct file * file, char __user * buf,
474 size_t count, loff_t *ppos)
479 static ssize_t write_null(struct file * file, const char __user * buf,
480 size_t count, loff_t *ppos)
485 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
486 struct splice_desc *sd)
491 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
492 loff_t *ppos, size_t len, unsigned int flags)
494 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
499 * For fun, we are using the MMU for this.
501 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
503 struct mm_struct *mm;
504 struct vm_area_struct * vma;
505 unsigned long addr=(unsigned long)buf;
508 /* Oops, this was forgotten before. -ben */
509 down_read(&mm->mmap_sem);
511 /* For private mappings, just map in zero pages. */
512 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
515 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
517 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
519 count = vma->vm_end - addr;
523 zap_page_range(vma, addr, count, NULL);
524 zeromap_page_range(vma, addr, count, PAGE_COPY);
533 up_read(&mm->mmap_sem);
535 /* The shared case is hard. Let's do the conventional zeroing. */
537 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
539 return size + unwritten - PAGE_SIZE;
547 up_read(&mm->mmap_sem);
551 static ssize_t read_zero(struct file * file, char __user * buf,
552 size_t count, loff_t *ppos)
554 unsigned long left, unwritten, written = 0;
559 if (!access_ok(VERIFY_WRITE, buf, count))
564 /* do we want to be clever? Arbitrary cut-off */
565 if (count >= PAGE_SIZE*4) {
566 unsigned long partial;
568 /* How much left of the page? */
569 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
570 unwritten = clear_user(buf, partial);
571 written = partial - unwritten;
576 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
577 written += (left & PAGE_MASK) - unwritten;
580 buf += left & PAGE_MASK;
583 unwritten = clear_user(buf, left);
584 written += left - unwritten;
586 return written ? written : -EFAULT;
589 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
591 if (vma->vm_flags & VM_SHARED)
592 return shmem_zero_setup(vma);
593 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
597 #else /* CONFIG_MMU */
598 static ssize_t read_zero(struct file * file, char * buf,
599 size_t count, loff_t *ppos)
607 chunk = 4096; /* Just for latency reasons */
608 if (clear_user(buf, chunk))
617 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
621 #endif /* CONFIG_MMU */
623 static ssize_t write_full(struct file * file, const char __user * buf,
624 size_t count, loff_t *ppos)
630 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
631 * can fopen() both devices with "a" now. This was previously impossible.
635 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
637 return file->f_pos = 0;
641 * The memory devices use the full 32/64 bits of the offset, and so we cannot
642 * check against negative addresses: they are ok. The return value is weird,
643 * though, in that case (0).
645 * also note that seeking relative to the "end of file" isn't supported:
646 * it has no meaning, so it returns -EINVAL.
648 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
652 mutex_lock(&file->f_dentry->d_inode->i_mutex);
655 file->f_pos = offset;
657 force_successful_syscall_return();
660 file->f_pos += offset;
662 force_successful_syscall_return();
667 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
671 static int open_port(struct inode * inode, struct file * filp)
673 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
676 #define zero_lseek null_lseek
677 #define full_lseek null_lseek
678 #define write_zero write_null
679 #define read_full read_zero
680 #define open_mem open_port
681 #define open_kmem open_mem
682 #define open_oldmem open_mem
684 #ifndef ARCH_HAS_DEV_MEM
685 static const struct file_operations mem_fops = {
686 .llseek = memory_lseek,
693 extern struct file_operations mem_fops;
696 static const struct file_operations kmem_fops = {
697 .llseek = memory_lseek,
703 static const struct file_operations null_fops = {
704 .llseek = null_lseek,
707 .splice_write = splice_write_null,
710 #if defined(CONFIG_ISA) || !defined(__mc68000__)
711 static const struct file_operations port_fops = {
712 .llseek = memory_lseek,
719 static const struct file_operations zero_fops = {
720 .llseek = zero_lseek,
726 static struct backing_dev_info zero_bdi = {
727 .capabilities = BDI_CAP_MAP_COPY,
730 static const struct file_operations full_fops = {
731 .llseek = full_lseek,
736 #ifdef CONFIG_CRASH_DUMP
737 static const struct file_operations oldmem_fops = {
743 static ssize_t kmsg_write(struct file * file, const char __user * buf,
744 size_t count, loff_t *ppos)
749 tmp = kmalloc(count + 1, GFP_KERNEL);
753 if (!copy_from_user(tmp, buf, count)) {
755 ret = printk("%s", tmp);
757 /* printk can add a prefix */
764 static const struct file_operations kmsg_fops = {
768 static int memory_open(struct inode * inode, struct file * filp)
770 switch (iminor(inode)) {
772 filp->f_op = &mem_fops;
775 filp->f_op = &kmem_fops;
778 filp->f_op = &null_fops;
780 #if defined(CONFIG_ISA) || !defined(__mc68000__)
782 filp->f_op = &port_fops;
786 filp->f_mapping->backing_dev_info = &zero_bdi;
787 filp->f_op = &zero_fops;
790 filp->f_op = &full_fops;
793 filp->f_op = &random_fops;
796 filp->f_op = &urandom_fops;
799 filp->f_op = &kmsg_fops;
801 #ifdef CONFIG_CRASH_DUMP
803 filp->f_op = &oldmem_fops;
809 if (filp->f_op && filp->f_op->open)
810 return filp->f_op->open(inode,filp);
814 static const struct file_operations memory_fops = {
815 .open = memory_open, /* just a selector for the real open */
818 static const struct {
822 const struct file_operations *fops;
823 } devlist[] = { /* list of minor devices */
824 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
825 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
826 #if defined(CONFIG_ISA) || !defined(__mc68000__)
827 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
829 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
830 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
831 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
832 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
833 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
834 #ifdef CONFIG_CRASH_DUMP
835 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
839 static struct class *mem_class;
841 static int __init chr_dev_init(void)
845 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
846 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
848 mem_class = class_create(THIS_MODULE, "mem");
849 for (i = 0; i < ARRAY_SIZE(devlist); i++)
850 class_device_create(mem_class, NULL,
851 MKDEV(MEM_MAJOR, devlist[i].minor),
852 NULL, devlist[i].name);
857 fs_initcall(chr_dev_init);