Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / mem.c
blob7f1464f3e6b81c64b64d01270269b4d86c77fa14
1 /*
2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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 */
11 #include <linux/config.h>
12 #include <linux/mm.h>
13 #include <linux/miscdevice.h>
14 #include <linux/tpqic02.h>
15 #include <linux/ftape.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mman.h>
19 #include <linux/random.h>
20 #include <linux/init.h>
21 #include <linux/raw.h>
22 #include <linux/tty.h>
23 #include <linux/capability.h>
24 #include <linux/smp_lock.h>
25 #include <linux/devfs_fs_kernel.h>
26 #include <linux/ptrace.h>
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30 #include <asm/pgalloc.h>
32 #ifdef CONFIG_IA64
33 # include <linux/efi.h>
34 #endif
36 #ifdef CONFIG_FB
37 extern void fbmem_init(void);
38 #endif
39 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
40 extern void tapechar_init(void);
41 #endif
44 * Architectures vary in how they handle caching for addresses
45 * outside of main memory.
48 static inline int uncached_access(struct file *file, unsigned long addr)
50 #if defined(__i386__)
52 * On the PPro and successors, the MTRRs are used to set
53 * memory types for physical addresses outside main memory,
54 * so blindly setting PCD or PWT on those pages is wrong.
55 * For Pentiums and earlier, the surround logic should disable
56 * caching for the high addresses through the KEN pin, but
57 * we maintain the tradition of paranoia in this code.
59 if (file->f_flags & O_SYNC)
60 return 1;
61 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
62 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
63 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
64 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
65 && addr >= __pa(high_memory);
66 #elif defined(CONFIG_IA64)
68 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
70 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
71 #else
73 * Accessing memory above the top the kernel knows about or through a file pointer
74 * that was marked O_SYNC will be done non-cached.
76 if (file->f_flags & O_SYNC)
77 return 1;
78 return addr >= __pa(high_memory);
79 #endif
82 static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,
83 const char * buf, size_t count, loff_t *ppos)
85 ssize_t written;
87 written = 0;
88 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
89 /* we don't have page 0 mapped on sparc and m68k.. */
90 if (realp < PAGE_SIZE) {
91 unsigned long sz = PAGE_SIZE-realp;
92 if (sz > count) sz = count;
93 /* Hmm. Do something? */
94 buf+=sz;
95 p+=sz;
96 count-=sz;
97 written+=sz;
99 #endif
100 if (copy_from_user(p, buf, count))
101 return -EFAULT;
102 written += count;
103 *ppos += written;
104 return written;
109 * This funcion reads the *physical* memory. The f_pos points directly to the
110 * memory location.
112 static ssize_t read_mem(struct file * file, char * buf,
113 size_t count, loff_t *ppos)
115 unsigned long p = *ppos;
116 unsigned long end_mem;
117 ssize_t read;
119 end_mem = __pa(high_memory);
120 if (p >= end_mem)
121 return 0;
122 if (count > end_mem - p)
123 count = end_mem - p;
124 read = 0;
125 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
126 /* we don't have page 0 mapped on sparc and m68k.. */
127 if (p < PAGE_SIZE) {
128 unsigned long sz = PAGE_SIZE-p;
129 if (sz > count)
130 sz = count;
131 if (sz > 0) {
132 if (clear_user(buf, sz))
133 return -EFAULT;
134 buf += sz;
135 p += sz;
136 count -= sz;
137 read += sz;
140 #endif
141 if (copy_to_user(buf, __va(p), count))
142 return -EFAULT;
143 read += count;
144 *ppos += read;
145 return read;
148 static ssize_t write_mem(struct file * file, const char * buf,
149 size_t count, loff_t *ppos)
151 unsigned long p = *ppos;
152 unsigned long end_mem;
154 end_mem = __pa(high_memory);
155 if (p >= end_mem)
156 return 0;
157 if (count > end_mem - p)
158 count = end_mem - p;
159 return do_write_mem(file, __va(p), p, buf, count, ppos);
162 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
164 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
165 int uncached;
167 uncached = uncached_access(file, offset);
168 #ifdef pgprot_noncached
169 if (uncached)
170 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
171 #endif
173 /* Don't try to swap out physical pages.. */
174 vma->vm_flags |= VM_RESERVED;
177 * Don't dump addresses that are not real memory to a core file.
179 if (uncached)
180 vma->vm_flags |= VM_IO;
182 if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,
183 vma->vm_page_prot))
184 return -EAGAIN;
185 return 0;
188 extern long vread(char *buf, char *addr, unsigned long count);
189 extern long vwrite(char *buf, char *addr, unsigned long count);
192 * This function reads the *virtual* memory as seen by the kernel.
194 static ssize_t read_kmem(struct file *file, char *buf,
195 size_t count, loff_t *ppos)
197 unsigned long p = *ppos;
198 ssize_t read = 0;
199 ssize_t virtr = 0;
200 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
202 if (p < (unsigned long) high_memory) {
203 read = count;
204 if (count > (unsigned long) high_memory - p)
205 read = (unsigned long) high_memory - p;
207 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
208 /* we don't have page 0 mapped on sparc and m68k.. */
209 if (p < PAGE_SIZE && read > 0) {
210 size_t tmp = PAGE_SIZE - p;
211 if (tmp > read) tmp = read;
212 if (clear_user(buf, tmp))
213 return -EFAULT;
214 buf += tmp;
215 p += tmp;
216 read -= tmp;
217 count -= tmp;
219 #endif
220 if (copy_to_user(buf, (char *)p, read))
221 return -EFAULT;
222 p += read;
223 buf += read;
224 count -= read;
227 if (count > 0) {
228 kbuf = (char *)__get_free_page(GFP_KERNEL);
229 if (!kbuf)
230 return -ENOMEM;
231 while (count > 0) {
232 int len = count;
234 if (len > PAGE_SIZE)
235 len = PAGE_SIZE;
236 len = vread(kbuf, (char *)p, len);
237 if (!len)
238 break;
239 if (copy_to_user(buf, kbuf, len)) {
240 free_page((unsigned long)kbuf);
241 return -EFAULT;
243 count -= len;
244 buf += len;
245 virtr += len;
246 p += len;
248 free_page((unsigned long)kbuf);
250 *ppos = p;
251 return virtr + read;
255 * This function writes to the *virtual* memory as seen by the kernel.
257 static ssize_t write_kmem(struct file * file, const char * buf,
258 size_t count, loff_t *ppos)
260 unsigned long p = *ppos;
261 ssize_t wrote = 0;
262 ssize_t virtr = 0;
263 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
265 if (p < (unsigned long) high_memory) {
266 wrote = count;
267 if (count > (unsigned long) high_memory - p)
268 wrote = (unsigned long) high_memory - p;
270 wrote = do_write_mem(file, (void*)p, p, buf, wrote, ppos);
272 p += wrote;
273 buf += wrote;
274 count -= wrote;
277 if (count > 0) {
278 kbuf = (char *)__get_free_page(GFP_KERNEL);
279 if (!kbuf)
280 return -ENOMEM;
281 while (count > 0) {
282 int len = count;
284 if (len > PAGE_SIZE)
285 len = PAGE_SIZE;
286 if (len && copy_from_user(kbuf, buf, len)) {
287 free_page((unsigned long)kbuf);
288 return -EFAULT;
290 len = vwrite(kbuf, (char *)p, len);
291 count -= len;
292 buf += len;
293 virtr += len;
294 p += len;
296 free_page((unsigned long)kbuf);
299 *ppos = p;
300 return virtr + wrote;
303 #if defined(CONFIG_ISA) || !defined(__mc68000__)
304 static ssize_t read_port(struct file * file, char * buf,
305 size_t count, loff_t *ppos)
307 unsigned long i = *ppos;
308 char *tmp = buf;
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++;
318 *ppos = i;
319 return tmp-buf;
322 static ssize_t write_port(struct file * file, const char * buf,
323 size_t count, loff_t *ppos)
325 unsigned long i = *ppos;
326 const char * tmp = buf;
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++;
338 *ppos = i;
339 return tmp-buf;
341 #endif
343 static ssize_t read_null(struct file * file, char * buf,
344 size_t count, loff_t *ppos)
346 return 0;
349 static ssize_t write_null(struct file * file, const char * buf,
350 size_t count, loff_t *ppos)
352 return count;
355 #ifdef CONFIG_MMU
357 * For fun, we are using the MMU for this.
359 static inline size_t read_zero_pagealigned(char * buf, size_t size)
361 struct mm_struct *mm;
362 struct vm_area_struct * vma;
363 unsigned long addr=(unsigned long)buf;
365 mm = current->mm;
366 /* Oops, this was forgotten before. -ben */
367 down_read(&mm->mmap_sem);
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;
373 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
374 goto out_up;
375 if (vma->vm_flags & VM_SHARED)
376 break;
377 count = vma->vm_end - addr;
378 if (count > size)
379 count = size;
381 zap_page_range(vma, addr, count);
382 zeromap_page_range(vma, addr, count, PAGE_COPY);
384 size -= count;
385 buf += count;
386 addr += count;
387 if (size == 0)
388 goto out_up;
391 up_read(&mm->mmap_sem);
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);
403 return size;
404 out_up:
405 up_read(&mm->mmap_sem);
406 return size;
409 static ssize_t read_zero(struct file * file, char * buf,
410 size_t count, loff_t *ppos)
412 unsigned long left, unwritten, written = 0;
414 if (!count)
415 return 0;
417 if (!access_ok(VERIFY_WRITE, buf, count))
418 return -EFAULT;
420 left = count;
422 /* do we want to be clever? Arbitrary cut-off */
423 if (count >= PAGE_SIZE*4) {
424 unsigned long partial;
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;
441 unwritten = clear_user(buf, left);
442 written += left - unwritten;
443 out:
444 return written ? written : -EFAULT;
447 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
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;
455 #else /* CONFIG_MMU */
456 static ssize_t read_zero(struct file * file, char * buf,
457 size_t count, loff_t *ppos)
459 size_t todo = count;
461 while (todo) {
462 size_t chunk = todo;
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();
472 return count;
475 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
477 return -ENOSYS;
479 #endif /* CONFIG_MMU */
481 static ssize_t write_full(struct file * file, const char * buf,
482 size_t count, loff_t *ppos)
484 return -ENOSPC;
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.
493 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
495 return file->f_pos = 0;
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).
503 * also note that seeking relative to the "end of file" isn't supported:
504 * it has no meaning, so it returns -EINVAL.
506 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
508 loff_t ret;
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;
525 up(&file->f_dentry->d_inode->i_sem);
526 return ret;
529 static int open_port(struct inode * inode, struct file * filp)
531 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
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
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,
550 static struct file_operations kmem_fops = {
551 .llseek = memory_lseek,
552 .read = read_kmem,
553 .write = write_kmem,
554 .mmap = mmap_kmem,
555 .open = open_kmem,
558 static struct file_operations null_fops = {
559 .llseek = null_lseek,
560 .read = read_null,
561 .write = write_null,
564 #if defined(CONFIG_ISA) || !defined(__mc68000__)
565 static struct file_operations port_fops = {
566 .llseek = memory_lseek,
567 .read = read_port,
568 .write = write_port,
569 .open = open_port,
571 #endif
573 static struct file_operations zero_fops = {
574 .llseek = zero_lseek,
575 .read = read_zero,
576 .write = write_zero,
577 .mmap = mmap_zero,
580 static struct file_operations full_fops = {
581 .llseek = full_lseek,
582 .read = read_full,
583 .write = write_full,
586 static ssize_t kmsg_write(struct file * file, const char * buf,
587 size_t count, loff_t *ppos)
589 char *tmp;
590 int ret;
592 tmp = kmalloc(count + 1, GFP_KERNEL);
593 if (tmp == NULL)
594 return -ENOMEM;
595 ret = -EFAULT;
596 if (!copy_from_user(tmp, buf, count)) {
597 tmp[count] = 0;
598 ret = printk("%s", tmp);
600 kfree(tmp);
601 return ret;
604 static struct file_operations kmsg_fops = {
605 .write = kmsg_write,
608 static int memory_open(struct inode * inode, struct file * filp)
610 switch (minor(inode->i_rdev)) {
611 case 1:
612 filp->f_op = &mem_fops;
613 break;
614 case 2:
615 filp->f_op = &kmem_fops;
616 break;
617 case 3:
618 filp->f_op = &null_fops;
619 break;
620 #if defined(CONFIG_ISA) || !defined(__mc68000__)
621 case 4:
622 filp->f_op = &port_fops;
623 break;
624 #endif
625 case 5:
626 filp->f_op = &zero_fops;
627 break;
628 case 7:
629 filp->f_op = &full_fops;
630 break;
631 case 8:
632 filp->f_op = &random_fops;
633 break;
634 case 9:
635 filp->f_op = &urandom_fops;
636 break;
637 case 11:
638 filp->f_op = &kmsg_fops;
639 break;
640 default:
641 return -ENXIO;
643 if (filp->f_op && filp->f_op->open)
644 return filp->f_op->open(inode,filp);
645 return 0;
648 static struct file_operations memory_fops = {
649 .open = memory_open, /* just a selector for the real open */
652 static const struct {
653 unsigned int minor;
654 char *name;
655 umode_t mode;
656 struct file_operations *fops;
657 } devlist[] = { /* list of minor devices */
658 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
659 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
660 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
661 #if defined(CONFIG_ISA) || !defined(__mc68000__)
662 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
663 #endif
664 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
665 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
666 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
667 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
668 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
671 static int __init chr_dev_init(void)
673 int i;
675 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
676 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
678 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
679 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
680 S_IFCHR | devlist[i].mode, devlist[i].name);
683 rand_initialize();
684 #if defined (CONFIG_FB)
685 fbmem_init();
686 #endif
687 tty_init();
688 #ifdef CONFIG_M68K_PRINTER
689 lp_m68k_init();
690 #endif
691 misc_init();
692 #ifdef CONFIG_FTAPE
693 ftape_init();
694 #endif
695 return 0;
698 fs_initcall(chr_dev_init);