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(CONFIG_IA64)
67 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
69 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
70 #elif defined(CONFIG_PPC64)
71 /* On PPC64, we always do non-cacheable access to the IO hole and
72 * cacheable elsewhere. Cache paradox can checkstop the CPU and
73 * the high_memory heuristic below is wrong on machines with memory
74 * above the IO hole... Ah, and of course, XFree86 doesn't pass
75 * O_SYNC when mapping us to tap IO space. Surprised ?
77 return !page_is_ram(addr);
80 * Accessing memory above the top the kernel knows about or through a file pointer
81 * that was marked O_SYNC will be done non-cached.
83 if (file->f_flags & O_SYNC)
85 return addr >= __pa(high_memory);
89 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
90 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
92 unsigned long end_mem;
94 end_mem = __pa(high_memory);
98 if (*count > end_mem - addr)
99 *count = end_mem - addr;
105 static ssize_t do_write_mem(void *p, unsigned long realp,
106 const char * buf, size_t count, loff_t *ppos)
109 unsigned long copied;
112 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
113 /* we don't have page 0 mapped on sparc and m68k.. */
114 if (realp < PAGE_SIZE) {
115 unsigned long sz = PAGE_SIZE-realp;
116 if (sz > count) sz = count;
117 /* Hmm. Do something? */
124 copied = copy_from_user(p, buf, count);
126 ssize_t ret = written + (count - copied);
139 * This funcion reads the *physical* memory. The f_pos points directly to the
142 static ssize_t read_mem(struct file * file, char * buf,
143 size_t count, loff_t *ppos)
145 unsigned long p = *ppos;
148 if (!valid_phys_addr_range(p, &count))
151 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
152 /* we don't have page 0 mapped on sparc and m68k.. */
154 unsigned long sz = PAGE_SIZE-p;
158 if (clear_user(buf, sz))
167 if (copy_to_user(buf, __va(p), count))
174 static ssize_t write_mem(struct file * file, const char * buf,
175 size_t count, loff_t *ppos)
177 unsigned long p = *ppos;
179 if (!valid_phys_addr_range(p, &count))
181 return do_write_mem(__va(p), p, buf, count, ppos);
184 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
186 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
189 uncached = uncached_access(file, offset);
190 #ifdef pgprot_noncached
192 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
195 /* Don't try to swap out physical pages.. */
196 vma->vm_flags |= VM_RESERVED;
199 * Don't dump addresses that are not real memory to a core file.
202 vma->vm_flags |= VM_IO;
204 if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,
210 extern long vread(char *buf, char *addr, unsigned long count);
211 extern long vwrite(char *buf, char *addr, unsigned long count);
214 * This function reads the *virtual* memory as seen by the kernel.
216 static ssize_t read_kmem(struct file *file, char *buf,
217 size_t count, loff_t *ppos)
219 unsigned long p = *ppos;
222 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
224 if (p < (unsigned long) high_memory) {
226 if (count > (unsigned long) high_memory - p)
227 read = (unsigned long) high_memory - p;
229 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
230 /* we don't have page 0 mapped on sparc and m68k.. */
231 if (p < PAGE_SIZE && read > 0) {
232 size_t tmp = PAGE_SIZE - p;
233 if (tmp > read) tmp = read;
234 if (clear_user(buf, tmp))
242 if (copy_to_user(buf, (char *)p, read))
250 kbuf = (char *)__get_free_page(GFP_KERNEL);
258 len = vread(kbuf, (char *)p, len);
261 if (copy_to_user(buf, kbuf, len)) {
262 free_page((unsigned long)kbuf);
270 free_page((unsigned long)kbuf);
277 * This function writes to the *virtual* memory as seen by the kernel.
279 static ssize_t write_kmem(struct file * file, const char * buf,
280 size_t count, loff_t *ppos)
282 unsigned long p = *ppos;
286 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
288 if (p < (unsigned long) high_memory) {
291 if (count > (unsigned long) high_memory - p)
292 wrote = (unsigned long) high_memory - p;
294 written = do_write_mem((void*)p, p, buf, wrote, ppos);
295 if (written != wrote)
304 kbuf = (char *)__get_free_page(GFP_KERNEL);
306 return wrote ? wrote : -ENOMEM;
313 written = copy_from_user(kbuf, buf, len);
317 free_page((unsigned long)kbuf);
318 ret = wrote + virtr + (len - written);
319 return ret ? ret : -EFAULT;
322 len = vwrite(kbuf, (char *)p, len);
328 free_page((unsigned long)kbuf);
332 return virtr + wrote;
335 #if defined(CONFIG_ISA) || !defined(__mc68000__)
336 static ssize_t read_port(struct file * file, char * buf,
337 size_t count, loff_t *ppos)
339 unsigned long i = *ppos;
342 if (verify_area(VERIFY_WRITE,buf,count))
344 while (count-- > 0 && i < 65536) {
345 if (__put_user(inb(i),tmp) < 0)
354 static ssize_t write_port(struct file * file, const char * buf,
355 size_t count, loff_t *ppos)
357 unsigned long i = *ppos;
358 const char * tmp = buf;
360 if (verify_area(VERIFY_READ,buf,count))
362 while (count-- > 0 && i < 65536) {
364 if (__get_user(c, tmp))
375 static ssize_t read_null(struct file * file, char * buf,
376 size_t count, loff_t *ppos)
381 static ssize_t write_null(struct file * file, const char * buf,
382 size_t count, loff_t *ppos)
389 * For fun, we are using the MMU for this.
391 static inline size_t read_zero_pagealigned(char * buf, size_t size)
393 struct mm_struct *mm;
394 struct vm_area_struct * vma;
395 unsigned long addr=(unsigned long)buf;
398 /* Oops, this was forgotten before. -ben */
399 down_read(&mm->mmap_sem);
401 /* For private mappings, just map in zero pages. */
402 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
405 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
407 if (vma->vm_flags & VM_SHARED)
409 count = vma->vm_end - addr;
413 zap_page_range(vma, addr, count, NULL);
414 zeromap_page_range(vma, addr, count, PAGE_COPY);
423 up_read(&mm->mmap_sem);
425 /* The shared case is hard. Let's do the conventional zeroing. */
427 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
429 return size + unwritten - PAGE_SIZE;
437 up_read(&mm->mmap_sem);
441 static ssize_t read_zero(struct file * file, char * buf,
442 size_t count, loff_t *ppos)
444 unsigned long left, unwritten, written = 0;
449 if (!access_ok(VERIFY_WRITE, buf, count))
454 /* do we want to be clever? Arbitrary cut-off */
455 if (count >= PAGE_SIZE*4) {
456 unsigned long partial;
458 /* How much left of the page? */
459 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
460 unwritten = clear_user(buf, partial);
461 written = partial - unwritten;
466 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
467 written += (left & PAGE_MASK) - unwritten;
470 buf += left & PAGE_MASK;
473 unwritten = clear_user(buf, left);
474 written += left - unwritten;
476 return written ? written : -EFAULT;
479 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
481 if (vma->vm_flags & VM_SHARED)
482 return shmem_zero_setup(vma);
483 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
487 #else /* CONFIG_MMU */
488 static ssize_t read_zero(struct file * file, char * buf,
489 size_t count, loff_t *ppos)
497 chunk = 4096; /* Just for latency reasons */
498 if (clear_user(buf, chunk))
507 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
511 #endif /* CONFIG_MMU */
513 static ssize_t write_full(struct file * file, const char * buf,
514 size_t count, loff_t *ppos)
520 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
521 * can fopen() both devices with "a" now. This was previously impossible.
525 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
527 return file->f_pos = 0;
531 * The memory devices use the full 32/64 bits of the offset, and so we cannot
532 * check against negative addresses: they are ok. The return value is weird,
533 * though, in that case (0).
535 * also note that seeking relative to the "end of file" isn't supported:
536 * it has no meaning, so it returns -EINVAL.
538 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
542 down(&file->f_dentry->d_inode->i_sem);
545 file->f_pos = offset;
547 force_successful_syscall_return();
550 file->f_pos += offset;
552 force_successful_syscall_return();
557 up(&file->f_dentry->d_inode->i_sem);
561 static int open_port(struct inode * inode, struct file * filp)
563 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
566 #define mmap_kmem mmap_mem
567 #define zero_lseek null_lseek
568 #define full_lseek null_lseek
569 #define write_zero write_null
570 #define read_full read_zero
571 #define open_mem open_port
572 #define open_kmem open_mem
574 static struct file_operations mem_fops = {
575 .llseek = memory_lseek,
582 static struct file_operations kmem_fops = {
583 .llseek = memory_lseek,
590 static struct file_operations null_fops = {
591 .llseek = null_lseek,
596 #if defined(CONFIG_ISA) || !defined(__mc68000__)
597 static struct file_operations port_fops = {
598 .llseek = memory_lseek,
605 static struct file_operations zero_fops = {
606 .llseek = zero_lseek,
612 static struct file_operations full_fops = {
613 .llseek = full_lseek,
618 static ssize_t kmsg_write(struct file * file, const char * buf,
619 size_t count, loff_t *ppos)
624 tmp = kmalloc(count + 1, GFP_KERNEL);
628 if (!copy_from_user(tmp, buf, count)) {
630 ret = printk("%s", tmp);
636 static struct file_operations kmsg_fops = {
640 static int memory_open(struct inode * inode, struct file * filp)
642 switch (iminor(inode)) {
644 filp->f_op = &mem_fops;
647 filp->f_op = &kmem_fops;
650 filp->f_op = &null_fops;
652 #if defined(CONFIG_ISA) || !defined(__mc68000__)
654 filp->f_op = &port_fops;
658 filp->f_op = &zero_fops;
661 filp->f_op = &full_fops;
664 filp->f_op = &random_fops;
667 filp->f_op = &urandom_fops;
670 filp->f_op = &kmsg_fops;
675 if (filp->f_op && filp->f_op->open)
676 return filp->f_op->open(inode,filp);
680 static struct file_operations memory_fops = {
681 .open = memory_open, /* just a selector for the real open */
684 static const struct {
688 struct file_operations *fops;
689 } devlist[] = { /* list of minor devices */
690 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
691 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
692 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
693 #if defined(CONFIG_ISA) || !defined(__mc68000__)
694 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
696 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
697 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
698 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
699 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
700 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
703 static struct class_simple *mem_class;
705 static int __init chr_dev_init(void)
709 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
710 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
712 mem_class = class_simple_create(THIS_MODULE, "mem");
713 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
714 class_simple_device_add(mem_class,
715 MKDEV(MEM_MAJOR, devlist[i].minor),
716 NULL, devlist[i].name);
717 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
718 S_IFCHR | devlist[i].mode, devlist[i].name);
721 #if defined (CONFIG_FB)
727 fs_initcall(chr_dev_init);