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>
29 #include <asm/pgalloc.h>
32 # include <linux/efi.h>
36 extern void fbmem_init(void);
38 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
39 extern void tapechar_init(void);
43 * Architectures vary in how they handle caching for addresses
44 * outside of main memory.
47 static inline int uncached_access(struct file *file, unsigned long addr)
51 * On the PPro and successors, the MTRRs are used to set
52 * memory types for physical addresses outside main memory,
53 * so blindly setting PCD or PWT on those pages is wrong.
54 * For Pentiums and earlier, the surround logic should disable
55 * caching for the high addresses through the KEN pin, but
56 * we maintain the tradition of paranoia in this code.
58 if (file->f_flags & O_SYNC)
60 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
61 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
62 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
63 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
64 && addr >= __pa(high_memory);
65 #elif defined(__x86_64__)
67 * This is broken because it can generate memory type aliases,
68 * which can cause cache corruptions
69 * But it is only available for root and we have to be bug-to-bug
70 * compatible with i386.
72 if (file->f_flags & O_SYNC)
74 /* same behaviour as i386. PAT always set to cached and MTRRs control the
76 Hopefully a full PAT implementation will fix that soon. */
78 #elif defined(CONFIG_IA64)
80 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
82 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
83 #elif defined(CONFIG_PPC64)
84 /* On PPC64, we always do non-cacheable access to the IO hole and
85 * cacheable elsewhere. Cache paradox can checkstop the CPU and
86 * the high_memory heuristic below is wrong on machines with memory
87 * above the IO hole... Ah, and of course, XFree86 doesn't pass
88 * O_SYNC when mapping us to tap IO space. Surprised ?
90 return !page_is_ram(addr);
93 * Accessing memory above the top the kernel knows about or through a file pointer
94 * that was marked O_SYNC will be done non-cached.
96 if (file->f_flags & O_SYNC)
98 return addr >= __pa(high_memory);
102 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
103 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
105 unsigned long end_mem;
107 end_mem = __pa(high_memory);
111 if (*count > end_mem - addr)
112 *count = end_mem - addr;
118 extern int page_is_ram(unsigned long pagenr);
120 static inline int page_is_allowed(unsigned long pagenr)
125 if (!page_is_ram(pagenr))
127 printk("Access to 0x%lx by %s denied \n", pagenr << PAGE_SHIFT, current->comm);
134 static inline int range_is_allowed(unsigned long from, unsigned long to)
136 unsigned long cursor;
138 cursor = from >> PAGE_SHIFT;
139 while ( (cursor << PAGE_SHIFT) < to) {
140 if (!page_is_allowed(cursor))
146 static ssize_t do_write_mem(void *p, unsigned long realp,
147 const char __user * buf, size_t count, loff_t *ppos)
150 unsigned long copied;
153 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
154 /* we don't have page 0 mapped on sparc and m68k.. */
155 if (realp < PAGE_SIZE) {
156 unsigned long sz = PAGE_SIZE-realp;
157 if (sz > count) sz = count;
158 /* Hmm. Do something? */
165 if (!range_is_allowed(realp, realp+count))
167 copied = copy_from_user(p, buf, count);
169 ssize_t ret = written + (count - copied);
182 * This funcion reads the *physical* memory. The f_pos points directly to the
185 static ssize_t read_mem(struct file * file, char __user * buf,
186 size_t count, loff_t *ppos)
188 unsigned long p = *ppos;
191 if (!valid_phys_addr_range(p, &count))
194 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
195 /* we don't have page 0 mapped on sparc and m68k.. */
197 unsigned long sz = PAGE_SIZE-p;
201 if (clear_user(buf, sz))
210 if (!range_is_allowed(p, p+count))
212 if (copy_to_user(buf, __va(p), count))
219 static ssize_t write_mem(struct file * file, const char __user * buf,
220 size_t count, loff_t *ppos)
222 unsigned long p = *ppos;
224 if (!valid_phys_addr_range(p, &count))
226 return do_write_mem(__va(p), p, buf, count, ppos);
229 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
231 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
233 unsigned long cursor;
235 uncached = uncached_access(file, offset);
236 #ifdef pgprot_noncached
238 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
241 /* Don't try to swap out physical pages.. */
242 vma->vm_flags |= VM_RESERVED;
245 * Don't dump addresses that are not real memory to a core file.
248 vma->vm_flags |= VM_IO;
250 cursor = vma->vm_pgoff;
251 while ((cursor << PAGE_SHIFT) < offset + vma->vm_end-vma->vm_start) {
252 if (!page_is_allowed(cursor))
257 if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,
263 extern long vread(char *buf, char *addr, unsigned long count);
264 extern long vwrite(char *buf, char *addr, unsigned long count);
267 * This function reads the *virtual* memory as seen by the kernel.
269 static ssize_t read_kmem(struct file *file, char __user *buf,
270 size_t count, loff_t *ppos)
272 unsigned long p = *ppos;
275 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
279 if (p < (unsigned long) high_memory) {
281 if (count > (unsigned long) high_memory - p)
282 read = (unsigned long) high_memory - p;
284 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
285 /* we don't have page 0 mapped on sparc and m68k.. */
286 if (p < PAGE_SIZE && read > 0) {
287 size_t tmp = PAGE_SIZE - p;
288 if (tmp > read) tmp = read;
289 if (clear_user(buf, tmp))
297 if (copy_to_user(buf, (char *)p, read))
305 kbuf = (char *)__get_free_page(GFP_KERNEL);
313 len = vread(kbuf, (char *)p, len);
316 if (copy_to_user(buf, kbuf, len)) {
317 free_page((unsigned long)kbuf);
325 free_page((unsigned long)kbuf);
332 * This function writes to the *virtual* memory as seen by the kernel.
334 static ssize_t write_kmem(struct file * file, const char __user * buf,
335 size_t count, loff_t *ppos)
337 unsigned long p = *ppos;
341 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
345 if (p < (unsigned long) high_memory) {
348 if (count > (unsigned long) high_memory - p)
349 wrote = (unsigned long) high_memory - p;
351 written = do_write_mem((void*)p, p, buf, wrote, ppos);
352 if (written != wrote)
361 kbuf = (char *)__get_free_page(GFP_KERNEL);
363 return wrote ? wrote : -ENOMEM;
370 written = copy_from_user(kbuf, buf, len);
374 free_page((unsigned long)kbuf);
375 ret = wrote + virtr + (len - written);
376 return ret ? ret : -EFAULT;
379 len = vwrite(kbuf, (char *)p, len);
385 free_page((unsigned long)kbuf);
389 return virtr + wrote;
392 #if defined(CONFIG_ISA) || !defined(__mc68000__)
393 static ssize_t read_port(struct file * file, char __user * buf,
394 size_t count, loff_t *ppos)
396 unsigned long i = *ppos;
397 char __user *tmp = buf;
399 if (verify_area(VERIFY_WRITE,buf,count))
401 while (count-- > 0 && i < 65536) {
402 if (__put_user(inb(i),tmp) < 0)
411 static ssize_t write_port(struct file * file, const char __user * buf,
412 size_t count, loff_t *ppos)
414 unsigned long i = *ppos;
415 const char __user * tmp = buf;
417 if (verify_area(VERIFY_READ,buf,count))
419 while (count-- > 0 && i < 65536) {
421 if (__get_user(c, tmp))
432 static ssize_t read_null(struct file * file, char __user * buf,
433 size_t count, loff_t *ppos)
438 static ssize_t write_null(struct file * file, const char __user * buf,
439 size_t count, loff_t *ppos)
446 * For fun, we are using the MMU for this.
448 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
450 struct mm_struct *mm;
451 struct vm_area_struct * vma;
452 unsigned long addr=(unsigned long)buf;
455 /* Oops, this was forgotten before. -ben */
456 down_read(&mm->mmap_sem);
458 /* For private mappings, just map in zero pages. */
459 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
462 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
464 if (vma->vm_flags & VM_SHARED)
466 count = vma->vm_end - addr;
470 zap_page_range(vma, addr, count, NULL);
471 zeromap_page_range(vma, addr, count, PAGE_COPY);
480 up_read(&mm->mmap_sem);
482 /* The shared case is hard. Let's do the conventional zeroing. */
484 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
486 return size + unwritten - PAGE_SIZE;
494 up_read(&mm->mmap_sem);
498 static ssize_t read_zero(struct file * file, char __user * buf,
499 size_t count, loff_t *ppos)
501 unsigned long left, unwritten, written = 0;
506 if (!access_ok(VERIFY_WRITE, buf, count))
511 /* do we want to be clever? Arbitrary cut-off */
512 if (count >= PAGE_SIZE*4) {
513 unsigned long partial;
515 /* How much left of the page? */
516 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
517 unwritten = clear_user(buf, partial);
518 written = partial - unwritten;
523 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
524 written += (left & PAGE_MASK) - unwritten;
527 buf += left & PAGE_MASK;
530 unwritten = clear_user(buf, left);
531 written += left - unwritten;
533 return written ? written : -EFAULT;
536 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
538 if (vma->vm_flags & VM_SHARED)
539 return shmem_zero_setup(vma);
540 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
544 #else /* CONFIG_MMU */
545 static ssize_t read_zero(struct file * file, char * buf,
546 size_t count, loff_t *ppos)
554 chunk = 4096; /* Just for latency reasons */
555 if (clear_user(buf, chunk))
564 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
568 #endif /* CONFIG_MMU */
570 static ssize_t write_full(struct file * file, const char __user * buf,
571 size_t count, loff_t *ppos)
577 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
578 * can fopen() both devices with "a" now. This was previously impossible.
582 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
584 return file->f_pos = 0;
588 * The memory devices use the full 32/64 bits of the offset, and so we cannot
589 * check against negative addresses: they are ok. The return value is weird,
590 * though, in that case (0).
592 * also note that seeking relative to the "end of file" isn't supported:
593 * it has no meaning, so it returns -EINVAL.
595 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
599 down(&file->f_dentry->d_inode->i_sem);
602 file->f_pos = offset;
604 force_successful_syscall_return();
607 file->f_pos += offset;
609 force_successful_syscall_return();
614 up(&file->f_dentry->d_inode->i_sem);
618 static int open_port(struct inode * inode, struct file * filp)
620 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
623 #define mmap_kmem mmap_mem
624 #define zero_lseek null_lseek
625 #define full_lseek null_lseek
626 #define write_zero write_null
627 #define read_full read_zero
628 #define open_mem open_port
629 #define open_kmem open_mem
631 static struct file_operations mem_fops = {
632 .llseek = memory_lseek,
639 static struct file_operations kmem_fops = {
640 .llseek = memory_lseek,
647 static struct file_operations null_fops = {
648 .llseek = null_lseek,
653 #if defined(CONFIG_ISA) || !defined(__mc68000__)
654 static struct file_operations port_fops = {
655 .llseek = memory_lseek,
662 static struct file_operations zero_fops = {
663 .llseek = zero_lseek,
669 static struct file_operations full_fops = {
670 .llseek = full_lseek,
675 static ssize_t kmsg_write(struct file * file, const char __user * buf,
676 size_t count, loff_t *ppos)
681 tmp = kmalloc(count + 1, GFP_KERNEL);
685 if (!copy_from_user(tmp, buf, count)) {
687 ret = printk("%s", tmp);
693 static struct file_operations kmsg_fops = {
697 static int memory_open(struct inode * inode, struct file * filp)
699 switch (iminor(inode)) {
701 filp->f_op = &mem_fops;
704 filp->f_op = &kmem_fops;
707 filp->f_op = &null_fops;
709 #if defined(CONFIG_ISA) || !defined(__mc68000__)
711 filp->f_op = &port_fops;
715 filp->f_op = &zero_fops;
718 filp->f_op = &full_fops;
721 filp->f_op = &random_fops;
724 filp->f_op = &urandom_fops;
727 filp->f_op = &kmsg_fops;
732 if (filp->f_op && filp->f_op->open)
733 return filp->f_op->open(inode,filp);
737 static struct file_operations memory_fops = {
738 .open = memory_open, /* just a selector for the real open */
741 static const struct {
745 struct file_operations *fops;
746 } devlist[] = { /* list of minor devices */
747 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
748 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
749 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
750 #if defined(CONFIG_ISA) || !defined(__mc68000__)
751 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
753 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
754 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
755 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
756 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
757 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
760 static struct class_simple *mem_class;
762 static int __init chr_dev_init(void)
766 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
767 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
769 mem_class = class_simple_create(THIS_MODULE, "mem");
770 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
771 class_simple_device_add(mem_class,
772 MKDEV(MEM_MAJOR, devlist[i].minor),
773 NULL, devlist[i].name);
774 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
775 S_IFCHR | devlist[i].mode, devlist[i].name);
778 #if defined (CONFIG_FB)
784 fs_initcall(chr_dev_init);