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>
11 #include <linux/config.h>
13 #include <linux/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mman.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/raw.h>
20 #include <linux/tty.h>
21 #include <linux/capability.h>
22 #include <linux/smp_lock.h>
23 #include <linux/devfs_fs_kernel.h>
24 #include <linux/ptrace.h>
25 #include <linux/device.h>
27 #include <asm/uaccess.h>
31 # include <linux/efi.h>
35 extern void fbmem_init(void);
37 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
38 extern void tapechar_init(void);
42 * Architectures vary in how they handle caching for addresses
43 * outside of main memory.
46 static inline int uncached_access(struct file *file, unsigned long addr)
50 * On the PPro and successors, the MTRRs are used to set
51 * memory types for physical addresses outside main memory,
52 * so blindly setting PCD or PWT on those pages is wrong.
53 * For Pentiums and earlier, the surround logic should disable
54 * caching for the high addresses through the KEN pin, but
55 * we maintain the tradition of paranoia in this code.
57 if (file->f_flags & O_SYNC)
59 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
60 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
61 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
62 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
63 && addr >= __pa(high_memory);
64 #elif defined(__x86_64__)
66 * This is broken because it can generate memory type aliases,
67 * which can cause cache corruptions
68 * But it is only available for root and we have to be bug-to-bug
69 * compatible with i386.
71 if (file->f_flags & O_SYNC)
73 /* same behaviour as i386. PAT always set to cached and MTRRs control the
75 Hopefully a full PAT implementation will fix that soon. */
77 #elif defined(CONFIG_IA64)
79 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
81 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
82 #elif defined(CONFIG_PPC64)
83 /* On PPC64, we always do non-cacheable access to the IO hole and
84 * cacheable elsewhere. Cache paradox can checkstop the CPU and
85 * the high_memory heuristic below is wrong on machines with memory
86 * above the IO hole... Ah, and of course, XFree86 doesn't pass
87 * O_SYNC when mapping us to tap IO space. Surprised ?
89 return !page_is_ram(addr);
92 * Accessing memory above the top the kernel knows about or through a file pointer
93 * that was marked O_SYNC will be done non-cached.
95 if (file->f_flags & O_SYNC)
97 return addr >= __pa(high_memory);
101 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
102 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
104 unsigned long end_mem;
106 end_mem = __pa(high_memory);
110 if (*count > end_mem - addr)
111 *count = end_mem - addr;
117 extern int page_is_ram(unsigned long pagenr);
119 static inline int page_is_allowed(unsigned long pagenr)
124 if (!page_is_ram(pagenr))
126 printk("Access to 0x%lx by %s denied \n", pagenr << PAGE_SHIFT, current->comm);
133 static inline int range_is_allowed(unsigned long from, unsigned long to)
135 unsigned long cursor;
137 cursor = from >> PAGE_SHIFT;
138 while ( (cursor << PAGE_SHIFT) < to) {
139 if (!page_is_allowed(cursor))
145 static ssize_t do_write_mem(void *p, unsigned long realp,
146 const char __user * buf, size_t count, loff_t *ppos)
149 unsigned long copied;
152 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
153 /* we don't have page 0 mapped on sparc and m68k.. */
154 if (realp < PAGE_SIZE) {
155 unsigned long sz = PAGE_SIZE-realp;
156 if (sz > count) sz = count;
157 /* Hmm. Do something? */
164 if (!range_is_allowed(realp, realp+count))
166 copied = copy_from_user(p, buf, count);
168 ssize_t ret = written + (count - copied);
181 * This funcion reads the *physical* memory. The f_pos points directly to the
184 static ssize_t read_mem(struct file * file, char __user * buf,
185 size_t count, loff_t *ppos)
187 unsigned long p = *ppos;
190 if (!valid_phys_addr_range(p, &count))
193 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
194 /* we don't have page 0 mapped on sparc and m68k.. */
196 unsigned long sz = PAGE_SIZE-p;
200 if (clear_user(buf, sz))
209 if (!range_is_allowed(p, p+count))
211 if (copy_to_user(buf, __va(p), count))
218 static ssize_t write_mem(struct file * file, const char __user * buf,
219 size_t count, loff_t *ppos)
221 unsigned long p = *ppos;
223 if (!valid_phys_addr_range(p, &count))
225 return do_write_mem(__va(p), p, buf, count, ppos);
228 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
230 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
232 unsigned long cursor;
234 uncached = uncached_access(file, offset);
235 #ifdef pgprot_noncached
237 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
240 /* Don't try to swap out physical pages.. */
241 vma->vm_flags |= VM_RESERVED;
244 * Don't dump addresses that are not real memory to a core file.
247 vma->vm_flags |= VM_IO;
249 cursor = vma->vm_pgoff;
250 while ((cursor << PAGE_SHIFT) < offset + vma->vm_end-vma->vm_start) {
251 if (!page_is_allowed(cursor))
256 if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,
262 extern long vread(char *buf, char *addr, unsigned long count);
263 extern long vwrite(char *buf, char *addr, unsigned long count);
266 * This function reads the *virtual* memory as seen by the kernel.
268 static ssize_t read_kmem(struct file *file, char __user *buf,
269 size_t count, loff_t *ppos)
271 unsigned long p = *ppos;
274 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
278 if (p < (unsigned long) high_memory) {
280 if (count > (unsigned long) high_memory - p)
281 read = (unsigned long) high_memory - p;
283 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
284 /* we don't have page 0 mapped on sparc and m68k.. */
285 if (p < PAGE_SIZE && read > 0) {
286 size_t tmp = PAGE_SIZE - p;
287 if (tmp > read) tmp = read;
288 if (clear_user(buf, tmp))
296 if (copy_to_user(buf, (char *)p, read))
304 kbuf = (char *)__get_free_page(GFP_KERNEL);
312 len = vread(kbuf, (char *)p, len);
315 if (copy_to_user(buf, kbuf, len)) {
316 free_page((unsigned long)kbuf);
324 free_page((unsigned long)kbuf);
331 * This function writes to the *virtual* memory as seen by the kernel.
333 static ssize_t write_kmem(struct file * file, const char __user * buf,
334 size_t count, loff_t *ppos)
336 unsigned long p = *ppos;
340 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
344 if (p < (unsigned long) high_memory) {
347 if (count > (unsigned long) high_memory - p)
348 wrote = (unsigned long) high_memory - p;
350 written = do_write_mem((void*)p, p, buf, wrote, ppos);
351 if (written != wrote)
360 kbuf = (char *)__get_free_page(GFP_KERNEL);
362 return wrote ? wrote : -ENOMEM;
369 written = copy_from_user(kbuf, buf, len);
373 free_page((unsigned long)kbuf);
374 ret = wrote + virtr + (len - written);
375 return ret ? ret : -EFAULT;
378 len = vwrite(kbuf, (char *)p, len);
384 free_page((unsigned long)kbuf);
388 return virtr + wrote;
391 #if defined(CONFIG_ISA) || !defined(__mc68000__)
392 static ssize_t read_port(struct file * file, char __user * buf,
393 size_t count, loff_t *ppos)
395 unsigned long i = *ppos;
396 char __user *tmp = buf;
398 if (verify_area(VERIFY_WRITE,buf,count))
400 while (count-- > 0 && i < 65536) {
401 if (__put_user(inb(i),tmp) < 0)
410 static ssize_t write_port(struct file * file, const char __user * buf,
411 size_t count, loff_t *ppos)
413 unsigned long i = *ppos;
414 const char __user * tmp = buf;
416 if (verify_area(VERIFY_READ,buf,count))
418 while (count-- > 0 && i < 65536) {
420 if (__get_user(c, tmp))
431 static ssize_t read_null(struct file * file, char __user * buf,
432 size_t count, loff_t *ppos)
437 static ssize_t write_null(struct file * file, const char __user * buf,
438 size_t count, loff_t *ppos)
445 * For fun, we are using the MMU for this.
447 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
449 struct mm_struct *mm;
450 struct vm_area_struct * vma;
451 unsigned long addr=(unsigned long)buf;
454 /* Oops, this was forgotten before. -ben */
455 down_read(&mm->mmap_sem);
457 /* For private mappings, just map in zero pages. */
458 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
461 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
463 if (vma->vm_flags & VM_SHARED)
465 count = vma->vm_end - addr;
469 zap_page_range(vma, addr, count, NULL);
470 zeromap_page_range(vma, addr, count, PAGE_COPY);
479 up_read(&mm->mmap_sem);
481 /* The shared case is hard. Let's do the conventional zeroing. */
483 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
485 return size + unwritten - PAGE_SIZE;
493 up_read(&mm->mmap_sem);
497 static ssize_t read_zero(struct file * file, char __user * buf,
498 size_t count, loff_t *ppos)
500 unsigned long left, unwritten, written = 0;
505 if (!access_ok(VERIFY_WRITE, buf, count))
510 /* do we want to be clever? Arbitrary cut-off */
511 if (count >= PAGE_SIZE*4) {
512 unsigned long partial;
514 /* How much left of the page? */
515 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
516 unwritten = clear_user(buf, partial);
517 written = partial - unwritten;
522 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
523 written += (left & PAGE_MASK) - unwritten;
526 buf += left & PAGE_MASK;
529 unwritten = clear_user(buf, left);
530 written += left - unwritten;
532 return written ? written : -EFAULT;
535 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
537 if (vma->vm_flags & VM_SHARED)
538 return shmem_zero_setup(vma);
539 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
543 #else /* CONFIG_MMU */
544 static ssize_t read_zero(struct file * file, char * buf,
545 size_t count, loff_t *ppos)
553 chunk = 4096; /* Just for latency reasons */
554 if (clear_user(buf, chunk))
563 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
567 #endif /* CONFIG_MMU */
569 static ssize_t write_full(struct file * file, const char __user * buf,
570 size_t count, loff_t *ppos)
576 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
577 * can fopen() both devices with "a" now. This was previously impossible.
581 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
583 return file->f_pos = 0;
587 * The memory devices use the full 32/64 bits of the offset, and so we cannot
588 * check against negative addresses: they are ok. The return value is weird,
589 * though, in that case (0).
591 * also note that seeking relative to the "end of file" isn't supported:
592 * it has no meaning, so it returns -EINVAL.
594 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
598 down(&file->f_dentry->d_inode->i_sem);
601 file->f_pos = offset;
603 force_successful_syscall_return();
606 file->f_pos += offset;
608 force_successful_syscall_return();
613 up(&file->f_dentry->d_inode->i_sem);
617 static int open_port(struct inode * inode, struct file * filp)
619 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
622 #define mmap_kmem mmap_mem
623 #define zero_lseek null_lseek
624 #define full_lseek null_lseek
625 #define write_zero write_null
626 #define read_full read_zero
627 #define open_mem open_port
628 #define open_kmem open_mem
630 static struct file_operations mem_fops = {
631 .llseek = memory_lseek,
638 static struct file_operations kmem_fops = {
639 .llseek = memory_lseek,
646 static struct file_operations null_fops = {
647 .llseek = null_lseek,
652 #if defined(CONFIG_ISA) || !defined(__mc68000__)
653 static struct file_operations port_fops = {
654 .llseek = memory_lseek,
661 static struct file_operations zero_fops = {
662 .llseek = zero_lseek,
668 static struct file_operations full_fops = {
669 .llseek = full_lseek,
674 static ssize_t kmsg_write(struct file * file, const char __user * buf,
675 size_t count, loff_t *ppos)
680 tmp = kmalloc(count + 1, GFP_KERNEL);
684 if (!copy_from_user(tmp, buf, count)) {
686 ret = printk("%s", tmp);
692 static struct file_operations kmsg_fops = {
696 static int memory_open(struct inode * inode, struct file * filp)
698 switch (iminor(inode)) {
700 filp->f_op = &mem_fops;
703 filp->f_op = &kmem_fops;
706 filp->f_op = &null_fops;
708 #if defined(CONFIG_ISA) || !defined(__mc68000__)
710 filp->f_op = &port_fops;
714 filp->f_op = &zero_fops;
717 filp->f_op = &full_fops;
720 filp->f_op = &random_fops;
723 filp->f_op = &urandom_fops;
726 filp->f_op = &kmsg_fops;
731 if (filp->f_op && filp->f_op->open)
732 return filp->f_op->open(inode,filp);
736 static struct file_operations memory_fops = {
737 .open = memory_open, /* just a selector for the real open */
740 static const struct {
744 struct file_operations *fops;
745 } devlist[] = { /* list of minor devices */
746 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
747 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
748 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
749 #if defined(CONFIG_ISA) || !defined(__mc68000__)
750 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
752 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
753 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
754 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
755 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
756 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
759 static struct class_simple *mem_class;
761 static int __init chr_dev_init(void)
765 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
766 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
768 mem_class = class_simple_create(THIS_MODULE, "mem");
769 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
770 class_simple_device_add(mem_class,
771 MKDEV(MEM_MAJOR, devlist[i].minor),
772 NULL, devlist[i].name);
773 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
774 S_IFCHR | devlist[i].mode, devlist[i].name);
777 #if defined (CONFIG_FB)
783 fs_initcall(chr_dev_init);