This commit was manufactured by cvs2svn to create branch
[linux-2.6.git] / drivers / char / mem.c
1 /*
2  *  linux/drivers/char/mem.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Added devfs support. 
7  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8  *  Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9  */
10
11 #include <linux/config.h>
12 #include <linux/mm.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>
26
27 #include <asm/uaccess.h>
28 #include <asm/io.h>
29
30 #ifdef CONFIG_IA64
31 # include <linux/efi.h>
32 #endif
33
34 #ifdef CONFIG_FB
35 extern void fbmem_init(void);
36 #endif
37 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
38 extern void tapechar_init(void);
39 #endif
40
41 /*
42  * Architectures vary in how they handle caching for addresses
43  * outside of main memory.
44  *
45  */
46 static inline int uncached_access(struct file *file, unsigned long addr)
47 {
48 #if defined(__i386__)
49         /*
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.
56          */
57         if (file->f_flags & O_SYNC)
58                 return 1;
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__)
65         /* 
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.
70          */
71         if (file->f_flags & O_SYNC)
72                 return 1;
73         /* same behaviour as i386. PAT always set to cached and MTRRs control the
74            caching behaviour. 
75            Hopefully a full PAT implementation will fix that soon. */      
76         return 0;
77 #elif defined(CONFIG_IA64)
78         /*
79          * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
80          */
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 ?
88          */
89         return !page_is_ram(addr);
90 #else
91         /*
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.
94          */
95         if (file->f_flags & O_SYNC)
96                 return 1;
97         return addr >= __pa(high_memory);
98 #endif
99 }
100
101 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
102 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
103 {
104         unsigned long end_mem;
105
106         end_mem = __pa(high_memory);
107         if (addr >= end_mem)
108                 return 0;
109
110         if (*count > end_mem - addr)
111                 *count = end_mem - addr;
112
113         return 1;
114 }
115 #endif
116
117 static inline int range_is_allowed(unsigned long from, unsigned long to)
118 {
119         unsigned long cursor;
120         
121         cursor = from >> PAGE_SHIFT;
122         while ((cursor << PAGE_SHIFT) < to) {
123                 if (!devmem_is_allowed(cursor))
124                         return 0;
125                 cursor++;
126         }
127         return 1;
128 }
129 static ssize_t do_write_mem(void *p, unsigned long realp,
130                             const char __user * buf, size_t count, loff_t *ppos)
131 {
132         ssize_t written;
133         unsigned long copied;
134
135         written = 0;
136 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
137         /* we don't have page 0 mapped on sparc and m68k.. */
138         if (realp < PAGE_SIZE) {
139                 unsigned long sz = PAGE_SIZE-realp;
140                 if (sz > count) sz = count; 
141                 /* Hmm. Do something? */
142                 buf+=sz;
143                 p+=sz;
144                 count-=sz;
145                 written+=sz;
146         }
147 #endif
148         if (!range_is_allowed(realp, realp+count))
149                 return -EPERM;
150         copied = copy_from_user(p, buf, count);
151         if (copied) {
152                 ssize_t ret = written + (count - copied);
153
154                 if (ret)
155                         return ret;
156                 return -EFAULT;
157         }
158         written += count;
159         *ppos += written;
160         return written;
161 }
162
163
164 /*
165  * This funcion reads the *physical* memory. The f_pos points directly to the 
166  * memory location. 
167  */
168 static ssize_t read_mem(struct file * file, char __user * buf,
169                         size_t count, loff_t *ppos)
170 {
171         unsigned long p = *ppos;
172         ssize_t read;
173
174         if (!valid_phys_addr_range(p, &count))
175                 return -EFAULT;
176         read = 0;
177 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
178         /* we don't have page 0 mapped on sparc and m68k.. */
179         if (p < PAGE_SIZE) {
180                 unsigned long sz = PAGE_SIZE-p;
181                 if (sz > count) 
182                         sz = count; 
183                 if (sz > 0) {
184                         if (clear_user(buf, sz))
185                                 return -EFAULT;
186                         buf += sz; 
187                         p += sz; 
188                         count -= sz; 
189                         read += sz; 
190                 }
191         }
192 #endif
193         if (!range_is_allowed(p, p+count))
194                 return -EPERM;
195         if (copy_to_user(buf, __va(p), count))
196                 return -EFAULT;
197         read += count;
198         *ppos += read;
199         return read;
200 }
201
202 static ssize_t write_mem(struct file * file, const char __user * buf, 
203                          size_t count, loff_t *ppos)
204 {
205         unsigned long p = *ppos;
206
207         if (!valid_phys_addr_range(p, &count))
208                 return -EFAULT;
209         return do_write_mem(__va(p), p, buf, count, ppos);
210 }
211
212 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
213 {
214         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
215         int uncached;
216         unsigned long cursor;
217
218         uncached = uncached_access(file, offset);
219 #ifdef pgprot_noncached
220         if (uncached)
221                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
222 #endif
223
224         /* Don't try to swap out physical pages.. */
225         vma->vm_flags |= VM_RESERVED;
226
227         /*
228          * Don't dump addresses that are not real memory to a core file.
229          */
230         if (uncached)
231                 vma->vm_flags |= VM_IO;
232                 
233         cursor = vma->vm_pgoff;
234         while ((cursor << PAGE_SHIFT) < offset + vma->vm_end-vma->vm_start) {
235                 if (!devmem_is_allowed(cursor))
236                         return -EPERM;
237                 cursor++;
238         }
239
240         if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,
241                              vma->vm_page_prot))
242                 return -EAGAIN;
243         return 0;
244 }
245
246 extern long vread(char *buf, char *addr, unsigned long count);
247 extern long vwrite(char *buf, char *addr, unsigned long count);
248
249 /*
250  * This function reads the *virtual* memory as seen by the kernel.
251  */
252 static ssize_t read_kmem(struct file *file, char __user *buf, 
253                          size_t count, loff_t *ppos)
254 {
255         unsigned long p = *ppos;
256         ssize_t read = 0;
257         ssize_t virtr = 0;
258         char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
259         
260         return -EPERM;
261                 
262         if (p < (unsigned long) high_memory) {
263                 read = count;
264                 if (count > (unsigned long) high_memory - p)
265                         read = (unsigned long) high_memory - p;
266
267 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
268                 /* we don't have page 0 mapped on sparc and m68k.. */
269                 if (p < PAGE_SIZE && read > 0) {
270                         size_t tmp = PAGE_SIZE - p;
271                         if (tmp > read) tmp = read;
272                         if (clear_user(buf, tmp))
273                                 return -EFAULT;
274                         buf += tmp;
275                         p += tmp;
276                         read -= tmp;
277                         count -= tmp;
278                 }
279 #endif
280                 if (copy_to_user(buf, (char *)p, read))
281                         return -EFAULT;
282                 p += read;
283                 buf += read;
284                 count -= read;
285         }
286
287         if (count > 0) {
288                 kbuf = (char *)__get_free_page(GFP_KERNEL);
289                 if (!kbuf)
290                         return -ENOMEM;
291                 while (count > 0) {
292                         int len = count;
293
294                         if (len > PAGE_SIZE)
295                                 len = PAGE_SIZE;
296                         len = vread(kbuf, (char *)p, len);
297                         if (!len)
298                                 break;
299                         if (copy_to_user(buf, kbuf, len)) {
300                                 free_page((unsigned long)kbuf);
301                                 return -EFAULT;
302                         }
303                         count -= len;
304                         buf += len;
305                         virtr += len;
306                         p += len;
307                 }
308                 free_page((unsigned long)kbuf);
309         }
310         *ppos = p;
311         return virtr + read;
312 }
313
314 #if defined(CONFIG_ISA) || !defined(__mc68000__)
315 static ssize_t read_port(struct file * file, char __user * buf,
316                          size_t count, loff_t *ppos)
317 {
318         unsigned long i = *ppos;
319         char __user *tmp = buf;
320
321         if (verify_area(VERIFY_WRITE,buf,count))
322                 return -EFAULT; 
323         while (count-- > 0 && i < 65536) {
324                 if (__put_user(inb(i),tmp) < 0) 
325                         return -EFAULT;  
326                 i++;
327                 tmp++;
328         }
329         *ppos = i;
330         return tmp-buf;
331 }
332
333 static ssize_t write_port(struct file * file, const char __user * buf,
334                           size_t count, loff_t *ppos)
335 {
336         unsigned long i = *ppos;
337         const char __user * tmp = buf;
338
339         if (verify_area(VERIFY_READ,buf,count))
340                 return -EFAULT;
341         while (count-- > 0 && i < 65536) {
342                 char c;
343                 if (__get_user(c, tmp)) 
344                         return -EFAULT; 
345                 outb(c,i);
346                 i++;
347                 tmp++;
348         }
349         *ppos = i;
350         return tmp-buf;
351 }
352 #endif
353
354 static ssize_t read_null(struct file * file, char __user * buf,
355                          size_t count, loff_t *ppos)
356 {
357         return 0;
358 }
359
360 static ssize_t write_null(struct file * file, const char __user * buf,
361                           size_t count, loff_t *ppos)
362 {
363         return count;
364 }
365
366 #ifdef CONFIG_MMU
367 /*
368  * For fun, we are using the MMU for this.
369  */
370 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
371 {
372         struct mm_struct *mm;
373         struct vm_area_struct * vma;
374         unsigned long addr=(unsigned long)buf;
375
376         mm = current->mm;
377         /* Oops, this was forgotten before. -ben */
378         down_read(&mm->mmap_sem);
379
380         /* For private mappings, just map in zero pages. */
381         for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
382                 unsigned long count;
383
384                 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
385                         goto out_up;
386                 if (vma->vm_flags & VM_SHARED)
387                         break;
388                 count = vma->vm_end - addr;
389                 if (count > size)
390                         count = size;
391
392                 zap_page_range(vma, addr, count, NULL);
393                 zeromap_page_range(vma, addr, count, PAGE_COPY);
394
395                 size -= count;
396                 buf += count;
397                 addr += count;
398                 if (size == 0)
399                         goto out_up;
400         }
401
402         up_read(&mm->mmap_sem);
403         
404         /* The shared case is hard. Let's do the conventional zeroing. */ 
405         do {
406                 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
407                 if (unwritten)
408                         return size + unwritten - PAGE_SIZE;
409                 cond_resched();
410                 buf += PAGE_SIZE;
411                 size -= PAGE_SIZE;
412         } while (size);
413
414         return size;
415 out_up:
416         up_read(&mm->mmap_sem);
417         return size;
418 }
419
420 static ssize_t read_zero(struct file * file, char __user * buf, 
421                          size_t count, loff_t *ppos)
422 {
423         unsigned long left, unwritten, written = 0;
424
425         if (!count)
426                 return 0;
427
428         if (!access_ok(VERIFY_WRITE, buf, count))
429                 return -EFAULT;
430
431         left = count;
432
433         /* do we want to be clever? Arbitrary cut-off */
434         if (count >= PAGE_SIZE*4) {
435                 unsigned long partial;
436
437                 /* How much left of the page? */
438                 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
439                 unwritten = clear_user(buf, partial);
440                 written = partial - unwritten;
441                 if (unwritten)
442                         goto out;
443                 left -= partial;
444                 buf += partial;
445                 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
446                 written += (left & PAGE_MASK) - unwritten;
447                 if (unwritten)
448                         goto out;
449                 buf += left & PAGE_MASK;
450                 left &= ~PAGE_MASK;
451         }
452         unwritten = clear_user(buf, left);
453         written += left - unwritten;
454 out:
455         return written ? written : -EFAULT;
456 }
457
458 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
459 {
460         if (vma->vm_flags & VM_SHARED)
461                 return shmem_zero_setup(vma);
462         if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
463                 return -EAGAIN;
464         return 0;
465 }
466 #else /* CONFIG_MMU */
467 static ssize_t read_zero(struct file * file, char * buf, 
468                          size_t count, loff_t *ppos)
469 {
470         size_t todo = count;
471
472         while (todo) {
473                 size_t chunk = todo;
474
475                 if (chunk > 4096)
476                         chunk = 4096;   /* Just for latency reasons */
477                 if (clear_user(buf, chunk))
478                         return -EFAULT;
479                 buf += chunk;
480                 todo -= chunk;
481                 cond_resched();
482         }
483         return count;
484 }
485
486 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
487 {
488         return -ENOSYS;
489 }
490 #endif /* CONFIG_MMU */
491
492 static ssize_t write_full(struct file * file, const char __user * buf,
493                           size_t count, loff_t *ppos)
494 {
495         return -ENOSPC;
496 }
497
498 /*
499  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
500  * can fopen() both devices with "a" now.  This was previously impossible.
501  * -- SRB.
502  */
503
504 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
505 {
506         return file->f_pos = 0;
507 }
508
509 /*
510  * The memory devices use the full 32/64 bits of the offset, and so we cannot
511  * check against negative addresses: they are ok. The return value is weird,
512  * though, in that case (0).
513  *
514  * also note that seeking relative to the "end of file" isn't supported:
515  * it has no meaning, so it returns -EINVAL.
516  */
517 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
518 {
519         loff_t ret;
520
521         down(&file->f_dentry->d_inode->i_sem);
522         switch (orig) {
523                 case 0:
524                         file->f_pos = offset;
525                         ret = file->f_pos;
526                         force_successful_syscall_return();
527                         break;
528                 case 1:
529                         file->f_pos += offset;
530                         ret = file->f_pos;
531                         force_successful_syscall_return();
532                         break;
533                 default:
534                         ret = -EINVAL;
535         }
536         up(&file->f_dentry->d_inode->i_sem);
537         return ret;
538 }
539
540 static int open_port(struct inode * inode, struct file * filp)
541 {
542         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
543 }
544
545 #define mmap_kmem       mmap_mem
546 #define zero_lseek      null_lseek
547 #define full_lseek      null_lseek
548 #define write_zero      write_null
549 #define read_full       read_zero
550 #define open_mem        open_port
551 #define open_kmem       open_mem
552
553 static struct file_operations mem_fops = {
554         .llseek         = memory_lseek,
555         .read           = read_mem,
556         .write          = write_mem,
557         .mmap           = mmap_mem,
558         .open           = open_mem,
559 };
560
561 static struct file_operations kmem_fops = {
562         .llseek         = memory_lseek,
563         .read           = read_kmem,
564         .mmap           = mmap_kmem,
565         .open           = open_kmem,
566 };
567
568 static struct file_operations null_fops = {
569         .llseek         = null_lseek,
570         .read           = read_null,
571         .write          = write_null,
572 };
573
574 #if defined(CONFIG_ISA) || !defined(__mc68000__)
575 static struct file_operations port_fops = {
576         .llseek         = memory_lseek,
577         .read           = read_port,
578         .write          = write_port,
579         .open           = open_port,
580 };
581 #endif
582
583 static struct file_operations zero_fops = {
584         .llseek         = zero_lseek,
585         .read           = read_zero,
586         .write          = write_zero,
587         .mmap           = mmap_zero,
588 };
589
590 static struct file_operations full_fops = {
591         .llseek         = full_lseek,
592         .read           = read_full,
593         .write          = write_full,
594 };
595
596 static ssize_t kmsg_write(struct file * file, const char __user * buf,
597                           size_t count, loff_t *ppos)
598 {
599         char *tmp;
600         int ret;
601
602         tmp = kmalloc(count + 1, GFP_KERNEL);
603         if (tmp == NULL)
604                 return -ENOMEM;
605         ret = -EFAULT;
606         if (!copy_from_user(tmp, buf, count)) {
607                 tmp[count] = 0;
608                 ret = printk("%s", tmp);
609         }
610         kfree(tmp);
611         return ret;
612 }
613
614 static struct file_operations kmsg_fops = {
615         .write =        kmsg_write,
616 };
617
618 static int memory_open(struct inode * inode, struct file * filp)
619 {
620         switch (iminor(inode)) {
621                 case 1:
622                         filp->f_op = &mem_fops;
623                         break;
624                 case 2:
625                         filp->f_op = &kmem_fops;
626                         break;
627                 case 3:
628                         filp->f_op = &null_fops;
629                         break;
630 #if defined(CONFIG_ISA) || !defined(__mc68000__)
631                 case 4:
632                         filp->f_op = &port_fops;
633                         break;
634 #endif
635                 case 5:
636                         filp->f_op = &zero_fops;
637                         break;
638                 case 7:
639                         filp->f_op = &full_fops;
640                         break;
641                 case 8:
642                         filp->f_op = &random_fops;
643                         break;
644                 case 9:
645                         filp->f_op = &urandom_fops;
646                         break;
647                 case 11:
648                         filp->f_op = &kmsg_fops;
649                         break;
650                 default:
651                         return -ENXIO;
652         }
653         if (filp->f_op && filp->f_op->open)
654                 return filp->f_op->open(inode,filp);
655         return 0;
656 }
657
658 static struct file_operations memory_fops = {
659         .open           = memory_open,  /* just a selector for the real open */
660 };
661
662 static const struct {
663         unsigned int            minor;
664         char                    *name;
665         umode_t                 mode;
666         struct file_operations  *fops;
667 } devlist[] = { /* list of minor devices */
668         {1, "mem",     S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
669         {2, "kmem",    S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
670         {3, "null",    S_IRUGO | S_IWUGO,           &null_fops},
671 #if defined(CONFIG_ISA) || !defined(__mc68000__)
672         {4, "port",    S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
673 #endif
674         {5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops},
675         {7, "full",    S_IRUGO | S_IWUGO,           &full_fops},
676         {8, "random",  S_IRUGO | S_IWUSR,           &random_fops},
677         {9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops},
678         {11,"kmsg",    S_IRUGO | S_IWUSR,           &kmsg_fops},
679 };
680
681 static struct class_simple *mem_class;
682
683 static int __init chr_dev_init(void)
684 {
685         int i;
686
687         if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
688                 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
689
690         mem_class = class_simple_create(THIS_MODULE, "mem");
691         for (i = 0; i < ARRAY_SIZE(devlist); i++) {
692                 class_simple_device_add(mem_class,
693                                         MKDEV(MEM_MAJOR, devlist[i].minor),
694                                         NULL, devlist[i].name);
695                 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
696                                 S_IFCHR | devlist[i].mode, devlist[i].name);
697         }
698         
699 #if defined (CONFIG_FB)
700         fbmem_init();
701 #endif
702         return 0;
703 }
704
705 fs_initcall(chr_dev_init);