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