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