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 static ssize_t do_write_mem(void *p, unsigned long realp,
119 const char __user * buf, size_t count, loff_t *ppos)
122 unsigned long copied;
125 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
126 /* we don't have page 0 mapped on sparc and m68k.. */
127 if (realp < PAGE_SIZE) {
128 unsigned long sz = PAGE_SIZE-realp;
129 if (sz > count) sz = count;
130 /* Hmm. Do something? */
137 copied = copy_from_user(p, buf, count);
139 ssize_t ret = written + (count - copied);
152 * This funcion reads the *physical* memory. The f_pos points directly to the
155 static ssize_t read_mem(struct file * file, char __user * buf,
156 size_t count, loff_t *ppos)
158 unsigned long p = *ppos;
161 if (!valid_phys_addr_range(p, &count))
164 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
165 /* we don't have page 0 mapped on sparc and m68k.. */
167 unsigned long sz = PAGE_SIZE-p;
171 if (clear_user(buf, sz))
180 if (copy_to_user(buf, __va(p), count))
187 static ssize_t write_mem(struct file * file, const char __user * buf,
188 size_t count, loff_t *ppos)
190 unsigned long p = *ppos;
192 if (!valid_phys_addr_range(p, &count))
194 return do_write_mem(__va(p), p, buf, count, ppos);
197 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
199 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
202 uncached = uncached_access(file, offset);
203 #ifdef pgprot_noncached
205 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
208 /* Don't try to swap out physical pages.. */
209 vma->vm_flags |= VM_RESERVED;
212 * Don't dump addresses that are not real memory to a core file.
215 vma->vm_flags |= VM_IO;
217 if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,
223 extern long vread(char *buf, char *addr, unsigned long count);
224 extern long vwrite(char *buf, char *addr, unsigned long count);
227 * This function reads the *virtual* memory as seen by the kernel.
229 static ssize_t read_kmem(struct file *file, char __user *buf,
230 size_t count, loff_t *ppos)
232 unsigned long p = *ppos;
235 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
237 if (p < (unsigned long) high_memory) {
239 if (count > (unsigned long) high_memory - p)
240 read = (unsigned long) high_memory - p;
242 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
243 /* we don't have page 0 mapped on sparc and m68k.. */
244 if (p < PAGE_SIZE && read > 0) {
245 size_t tmp = PAGE_SIZE - p;
246 if (tmp > read) tmp = read;
247 if (clear_user(buf, tmp))
255 if (copy_to_user(buf, (char *)p, read))
263 kbuf = (char *)__get_free_page(GFP_KERNEL);
271 len = vread(kbuf, (char *)p, len);
274 if (copy_to_user(buf, kbuf, len)) {
275 free_page((unsigned long)kbuf);
283 free_page((unsigned long)kbuf);
290 * This function writes to the *virtual* memory as seen by the kernel.
292 static ssize_t write_kmem(struct file * file, const char __user * buf,
293 size_t count, loff_t *ppos)
295 unsigned long p = *ppos;
299 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
301 if (p < (unsigned long) high_memory) {
304 if (count > (unsigned long) high_memory - p)
305 wrote = (unsigned long) high_memory - p;
307 written = do_write_mem((void*)p, p, buf, wrote, ppos);
308 if (written != wrote)
317 kbuf = (char *)__get_free_page(GFP_KERNEL);
319 return wrote ? wrote : -ENOMEM;
326 written = copy_from_user(kbuf, buf, len);
330 free_page((unsigned long)kbuf);
331 ret = wrote + virtr + (len - written);
332 return ret ? ret : -EFAULT;
335 len = vwrite(kbuf, (char *)p, len);
341 free_page((unsigned long)kbuf);
345 return virtr + wrote;
348 #if defined(CONFIG_ISA) || !defined(__mc68000__)
349 static ssize_t read_port(struct file * file, char __user * buf,
350 size_t count, loff_t *ppos)
352 unsigned long i = *ppos;
353 char __user *tmp = buf;
355 if (verify_area(VERIFY_WRITE,buf,count))
357 while (count-- > 0 && i < 65536) {
358 if (__put_user(inb(i),tmp) < 0)
367 static ssize_t write_port(struct file * file, const char __user * buf,
368 size_t count, loff_t *ppos)
370 unsigned long i = *ppos;
371 const char __user * tmp = buf;
373 if (verify_area(VERIFY_READ,buf,count))
375 while (count-- > 0 && i < 65536) {
377 if (__get_user(c, tmp))
388 static ssize_t read_null(struct file * file, char __user * buf,
389 size_t count, loff_t *ppos)
394 static ssize_t write_null(struct file * file, const char __user * buf,
395 size_t count, loff_t *ppos)
402 * For fun, we are using the MMU for this.
404 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
406 struct mm_struct *mm;
407 struct vm_area_struct * vma;
408 unsigned long addr=(unsigned long)buf;
411 /* Oops, this was forgotten before. -ben */
412 down_read(&mm->mmap_sem);
414 /* For private mappings, just map in zero pages. */
415 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
418 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
420 if (vma->vm_flags & VM_SHARED)
422 count = vma->vm_end - addr;
426 zap_page_range(vma, addr, count, NULL);
427 zeromap_page_range(vma, addr, count, PAGE_COPY);
436 up_read(&mm->mmap_sem);
438 /* The shared case is hard. Let's do the conventional zeroing. */
440 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
442 return size + unwritten - PAGE_SIZE;
450 up_read(&mm->mmap_sem);
454 static ssize_t read_zero(struct file * file, char __user * buf,
455 size_t count, loff_t *ppos)
457 unsigned long left, unwritten, written = 0;
462 if (!access_ok(VERIFY_WRITE, buf, count))
467 /* do we want to be clever? Arbitrary cut-off */
468 if (count >= PAGE_SIZE*4) {
469 unsigned long partial;
471 /* How much left of the page? */
472 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
473 unwritten = clear_user(buf, partial);
474 written = partial - unwritten;
479 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
480 written += (left & PAGE_MASK) - unwritten;
483 buf += left & PAGE_MASK;
486 unwritten = clear_user(buf, left);
487 written += left - unwritten;
489 return written ? written : -EFAULT;
492 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
494 if (vma->vm_flags & VM_SHARED)
495 return shmem_zero_setup(vma);
496 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
500 #else /* CONFIG_MMU */
501 static ssize_t read_zero(struct file * file, char * buf,
502 size_t count, loff_t *ppos)
510 chunk = 4096; /* Just for latency reasons */
511 if (clear_user(buf, chunk))
520 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
524 #endif /* CONFIG_MMU */
526 static ssize_t write_full(struct file * file, const char __user * buf,
527 size_t count, loff_t *ppos)
533 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
534 * can fopen() both devices with "a" now. This was previously impossible.
538 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
540 return file->f_pos = 0;
544 * The memory devices use the full 32/64 bits of the offset, and so we cannot
545 * check against negative addresses: they are ok. The return value is weird,
546 * though, in that case (0).
548 * also note that seeking relative to the "end of file" isn't supported:
549 * it has no meaning, so it returns -EINVAL.
551 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
555 down(&file->f_dentry->d_inode->i_sem);
558 file->f_pos = offset;
560 force_successful_syscall_return();
563 file->f_pos += offset;
565 force_successful_syscall_return();
570 up(&file->f_dentry->d_inode->i_sem);
574 static int open_port(struct inode * inode, struct file * filp)
576 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
579 #define mmap_kmem mmap_mem
580 #define zero_lseek null_lseek
581 #define full_lseek null_lseek
582 #define write_zero write_null
583 #define read_full read_zero
584 #define open_mem open_port
585 #define open_kmem open_mem
587 static struct file_operations mem_fops = {
588 .llseek = memory_lseek,
595 static struct file_operations kmem_fops = {
596 .llseek = memory_lseek,
603 static struct file_operations null_fops = {
604 .llseek = null_lseek,
609 #if defined(CONFIG_ISA) || !defined(__mc68000__)
610 static struct file_operations port_fops = {
611 .llseek = memory_lseek,
618 static struct file_operations zero_fops = {
619 .llseek = zero_lseek,
625 static struct file_operations full_fops = {
626 .llseek = full_lseek,
631 static ssize_t kmsg_write(struct file * file, const char __user * buf,
632 size_t count, loff_t *ppos)
637 tmp = kmalloc(count + 1, GFP_KERNEL);
641 if (!copy_from_user(tmp, buf, count)) {
643 ret = printk("%s", tmp);
649 static struct file_operations kmsg_fops = {
653 static int memory_open(struct inode * inode, struct file * filp)
655 switch (iminor(inode)) {
657 filp->f_op = &mem_fops;
660 filp->f_op = &kmem_fops;
663 filp->f_op = &null_fops;
665 #if defined(CONFIG_ISA) || !defined(__mc68000__)
667 filp->f_op = &port_fops;
671 filp->f_op = &zero_fops;
674 filp->f_op = &full_fops;
677 filp->f_op = &random_fops;
680 filp->f_op = &urandom_fops;
683 filp->f_op = &kmsg_fops;
688 if (filp->f_op && filp->f_op->open)
689 return filp->f_op->open(inode,filp);
693 static struct file_operations memory_fops = {
694 .open = memory_open, /* just a selector for the real open */
697 static const struct {
701 struct file_operations *fops;
702 } devlist[] = { /* list of minor devices */
703 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
704 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
705 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
706 #if defined(CONFIG_ISA) || !defined(__mc68000__)
707 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
709 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
710 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
711 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
712 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
713 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
716 static struct class_simple *mem_class;
718 static int __init chr_dev_init(void)
722 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
723 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
725 mem_class = class_simple_create(THIS_MODULE, "mem");
726 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
727 class_simple_device_add(mem_class,
728 MKDEV(MEM_MAJOR, devlist[i].minor),
729 NULL, devlist[i].name);
730 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
731 S_IFCHR | devlist[i].mode, devlist[i].name);
734 #if defined (CONFIG_FB)
740 fs_initcall(chr_dev_init);