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