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