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>
34 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
35 extern void tapechar_init(void);
39 * Architectures vary in how they handle caching for addresses
40 * outside of main memory.
43 static inline int uncached_access(struct file *file, unsigned long addr)
47 * On the PPro and successors, the MTRRs are used to set
48 * memory types for physical addresses outside main memory,
49 * so blindly setting PCD or PWT on those pages is wrong.
50 * For Pentiums and earlier, the surround logic should disable
51 * caching for the high addresses through the KEN pin, but
52 * we maintain the tradition of paranoia in this code.
54 if (file->f_flags & O_SYNC)
56 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
57 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
58 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
59 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
60 && addr >= __pa(high_memory);
61 #elif defined(__x86_64__)
63 * This is broken because it can generate memory type aliases,
64 * which can cause cache corruptions
65 * But it is only available for root and we have to be bug-to-bug
66 * compatible with i386.
68 if (file->f_flags & O_SYNC)
70 /* same behaviour as i386. PAT always set to cached and MTRRs control the
72 Hopefully a full PAT implementation will fix that soon. */
74 #elif defined(CONFIG_IA64)
76 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
78 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
79 #elif defined(CONFIG_PPC64)
80 /* On PPC64, we always do non-cacheable access to the IO hole and
81 * cacheable elsewhere. Cache paradox can checkstop the CPU and
82 * the high_memory heuristic below is wrong on machines with memory
83 * above the IO hole... Ah, and of course, XFree86 doesn't pass
84 * O_SYNC when mapping us to tap IO space. Surprised ?
86 return !page_is_ram(addr >> PAGE_SHIFT);
89 * Accessing memory above the top the kernel knows about or through a file pointer
90 * that was marked O_SYNC will be done non-cached.
92 if (file->f_flags & O_SYNC)
94 return addr >= __pa(high_memory);
98 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
99 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
101 unsigned long end_mem;
103 end_mem = __pa(high_memory);
107 if (*count > end_mem - addr)
108 *count = end_mem - addr;
114 static inline int range_is_allowed(unsigned long from, unsigned long to)
116 unsigned long cursor;
118 cursor = from >> PAGE_SHIFT;
119 while ((cursor << PAGE_SHIFT) < to) {
120 if (!devmem_is_allowed(cursor))
126 static ssize_t do_write_mem(void *p, unsigned long realp,
127 const char __user * buf, size_t count, loff_t *ppos)
130 unsigned long copied;
133 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
134 /* we don't have page 0 mapped on sparc and m68k.. */
135 if (realp < PAGE_SIZE) {
136 unsigned long sz = PAGE_SIZE-realp;
137 if (sz > count) sz = count;
138 /* Hmm. Do something? */
145 if (!range_is_allowed(realp, realp+count))
147 copied = copy_from_user(p, buf, count);
149 ssize_t ret = written + (count - copied);
162 * This funcion reads the *physical* memory. The f_pos points directly to the
165 static ssize_t read_mem(struct file * file, char __user * buf,
166 size_t count, loff_t *ppos)
168 unsigned long p = *ppos;
171 if (!valid_phys_addr_range(p, &count))
174 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
175 /* we don't have page 0 mapped on sparc and m68k.. */
177 unsigned long sz = PAGE_SIZE-p;
181 if (clear_user(buf, sz))
190 if (!range_is_allowed(p, p+count))
192 if (copy_to_user(buf, __va(p), count))
199 static ssize_t write_mem(struct file * file, const char __user * buf,
200 size_t count, loff_t *ppos)
202 unsigned long p = *ppos;
204 if (!valid_phys_addr_range(p, &count))
206 return do_write_mem(__va(p), p, buf, count, ppos);
209 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
211 #ifdef pgprot_noncached
212 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
215 uncached = uncached_access(file, offset);
217 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
220 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
221 if (remap_pfn_range(vma,
224 vma->vm_end-vma->vm_start,
230 extern long vread(char *buf, char *addr, unsigned long count);
231 extern long vwrite(char *buf, char *addr, unsigned long count);
234 * This function reads the *virtual* memory as seen by the kernel.
236 static ssize_t read_kmem(struct file *file, char __user *buf,
237 size_t count, loff_t *ppos)
239 unsigned long p = *ppos;
242 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
246 if (p < (unsigned long) high_memory) {
248 if (count > (unsigned long) high_memory - p)
249 read = (unsigned long) high_memory - p;
251 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
252 /* we don't have page 0 mapped on sparc and m68k.. */
253 if (p < PAGE_SIZE && read > 0) {
254 size_t tmp = PAGE_SIZE - p;
255 if (tmp > read) tmp = read;
256 if (clear_user(buf, tmp))
264 if (copy_to_user(buf, (char *)p, read))
272 kbuf = (char *)__get_free_page(GFP_KERNEL);
280 len = vread(kbuf, (char *)p, len);
283 if (copy_to_user(buf, kbuf, len)) {
284 free_page((unsigned long)kbuf);
292 free_page((unsigned long)kbuf);
298 #if defined(CONFIG_ISA) || !defined(__mc68000__)
299 static ssize_t read_port(struct file * file, char __user * buf,
300 size_t count, loff_t *ppos)
302 unsigned long i = *ppos;
303 char __user *tmp = buf;
305 if (verify_area(VERIFY_WRITE,buf,count))
307 while (count-- > 0 && i < 65536) {
308 if (__put_user(inb(i),tmp) < 0)
317 static ssize_t write_port(struct file * file, const char __user * buf,
318 size_t count, loff_t *ppos)
320 unsigned long i = *ppos;
321 const char __user * tmp = buf;
323 if (verify_area(VERIFY_READ,buf,count))
325 while (count-- > 0 && i < 65536) {
327 if (__get_user(c, tmp))
338 static ssize_t read_null(struct file * file, char __user * buf,
339 size_t count, loff_t *ppos)
344 static ssize_t write_null(struct file * file, const char __user * buf,
345 size_t count, loff_t *ppos)
352 * For fun, we are using the MMU for this.
354 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
356 struct mm_struct *mm;
357 struct vm_area_struct * vma;
358 unsigned long addr=(unsigned long)buf;
361 /* Oops, this was forgotten before. -ben */
362 down_read(&mm->mmap_sem);
364 /* For private mappings, just map in zero pages. */
365 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
368 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
370 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
372 count = vma->vm_end - addr;
376 zap_page_range(vma, addr, count, NULL);
377 zeromap_page_range(vma, addr, count, PAGE_COPY);
386 up_read(&mm->mmap_sem);
388 /* The shared case is hard. Let's do the conventional zeroing. */
390 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
392 return size + unwritten - PAGE_SIZE;
400 up_read(&mm->mmap_sem);
404 static ssize_t read_zero(struct file * file, char __user * buf,
405 size_t count, loff_t *ppos)
407 unsigned long left, unwritten, written = 0;
412 if (!access_ok(VERIFY_WRITE, buf, count))
417 /* do we want to be clever? Arbitrary cut-off */
418 if (count >= PAGE_SIZE*4) {
419 unsigned long partial;
421 /* How much left of the page? */
422 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
423 unwritten = clear_user(buf, partial);
424 written = partial - unwritten;
429 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
430 written += (left & PAGE_MASK) - unwritten;
433 buf += left & PAGE_MASK;
436 unwritten = clear_user(buf, left);
437 written += left - unwritten;
439 return written ? written : -EFAULT;
442 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
444 if (vma->vm_flags & VM_SHARED)
445 return shmem_zero_setup(vma);
446 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
450 #else /* CONFIG_MMU */
451 static ssize_t read_zero(struct file * file, char * buf,
452 size_t count, loff_t *ppos)
460 chunk = 4096; /* Just for latency reasons */
461 if (clear_user(buf, chunk))
470 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
474 #endif /* CONFIG_MMU */
476 static ssize_t write_full(struct file * file, const char __user * buf,
477 size_t count, loff_t *ppos)
483 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
484 * can fopen() both devices with "a" now. This was previously impossible.
488 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
490 return file->f_pos = 0;
494 * The memory devices use the full 32/64 bits of the offset, and so we cannot
495 * check against negative addresses: they are ok. The return value is weird,
496 * though, in that case (0).
498 * also note that seeking relative to the "end of file" isn't supported:
499 * it has no meaning, so it returns -EINVAL.
501 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
505 down(&file->f_dentry->d_inode->i_sem);
508 file->f_pos = offset;
510 force_successful_syscall_return();
513 file->f_pos += offset;
515 force_successful_syscall_return();
520 up(&file->f_dentry->d_inode->i_sem);
524 static int open_port(struct inode * inode, struct file * filp)
526 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
529 #define mmap_kmem mmap_mem
530 #define zero_lseek null_lseek
531 #define full_lseek null_lseek
532 #define write_zero write_null
533 #define read_full read_zero
534 #define open_mem open_port
535 #define open_kmem open_mem
537 static struct file_operations mem_fops = {
538 .llseek = memory_lseek,
545 static struct file_operations kmem_fops = {
546 .llseek = memory_lseek,
552 static struct file_operations null_fops = {
553 .llseek = null_lseek,
558 #if defined(CONFIG_ISA) || !defined(__mc68000__)
559 static struct file_operations port_fops = {
560 .llseek = memory_lseek,
567 static struct file_operations zero_fops = {
568 .llseek = zero_lseek,
574 static struct file_operations full_fops = {
575 .llseek = full_lseek,
580 static ssize_t kmsg_write(struct file * file, const char __user * buf,
581 size_t count, loff_t *ppos)
586 tmp = kmalloc(count + 1, GFP_KERNEL);
590 if (!copy_from_user(tmp, buf, count)) {
592 ret = printk("%s", tmp);
598 static struct file_operations kmsg_fops = {
602 static int memory_open(struct inode * inode, struct file * filp)
604 switch (iminor(inode)) {
606 filp->f_op = &mem_fops;
609 filp->f_op = &kmem_fops;
612 filp->f_op = &null_fops;
614 #if defined(CONFIG_ISA) || !defined(__mc68000__)
616 filp->f_op = &port_fops;
620 filp->f_op = &zero_fops;
623 filp->f_op = &full_fops;
626 filp->f_op = &random_fops;
629 filp->f_op = &urandom_fops;
632 filp->f_op = &kmsg_fops;
637 if (filp->f_op && filp->f_op->open)
638 return filp->f_op->open(inode,filp);
642 static struct file_operations memory_fops = {
643 .open = memory_open, /* just a selector for the real open */
646 static const struct {
650 struct file_operations *fops;
651 } devlist[] = { /* list of minor devices */
652 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
653 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
654 #if defined(CONFIG_ISA) || !defined(__mc68000__)
655 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
657 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
658 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
659 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
660 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
661 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
664 static struct class_simple *mem_class;
666 static int __init chr_dev_init(void)
670 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
671 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
673 mem_class = class_simple_create(THIS_MODULE, "mem");
674 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
675 class_simple_device_add(mem_class,
676 MKDEV(MEM_MAJOR, devlist[i].minor),
677 NULL, devlist[i].name);
678 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
679 S_IFCHR | devlist[i].mode, devlist[i].name);
685 fs_initcall(chr_dev_init);