ia64/xen-unstable

view linux-2.6.9-xen-sparse/drivers/char/mem.c @ 3004:2bebf77dc30e

bitkeeper revision 1.1159.175.1 (419a9b9dCZLKnt1tKvFYyV5cDeDbmQ)

Clean up drivers/char/mem.c patch.
author iap10@labyrinth.cl.cam.ac.uk
date Wed Nov 17 00:30:21 2004 +0000 (2004-11-17)
parents 145b7783c604
children d3865a06ff71
line source
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 */
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>
27 #include <asm/uaccess.h>
28 #include <asm/io.h>
29 #include <asm/pgalloc.h>
31 #ifdef CONFIG_IA64
32 # include <linux/efi.h>
33 #endif
35 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
36 extern void tapechar_init(void);
37 #endif
39 /*
40 * Architectures vary in how they handle caching for addresses
41 * outside of main memory.
42 *
43 */
44 static inline int uncached_access(struct file *file, unsigned long addr)
45 {
46 #ifdef CONFIG_XEN
47 if (file->f_flags & O_SYNC)
48 return 1;
49 /* Xen sets correct MTRR type on non-RAM for us. */
50 return 0;
51 #elif defined(__i386__)
52 /*
53 * On the PPro and successors, the MTRRs are used to set
54 * memory types for physical addresses outside main memory,
55 * so blindly setting PCD or PWT on those pages is wrong.
56 * For Pentiums and earlier, the surround logic should disable
57 * caching for the high addresses through the KEN pin, but
58 * we maintain the tradition of paranoia in this code.
59 */
60 if (file->f_flags & O_SYNC)
61 return 1;
62 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
63 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
64 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
65 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
66 && addr >= __pa(high_memory);
67 #elif defined(__x86_64__)
68 /*
69 * This is broken because it can generate memory type aliases,
70 * which can cause cache corruptions
71 * But it is only available for root and we have to be bug-to-bug
72 * compatible with i386.
73 */
74 if (file->f_flags & O_SYNC)
75 return 1;
76 /* same behaviour as i386. PAT always set to cached and MTRRs control the
77 caching behaviour.
78 Hopefully a full PAT implementation will fix that soon. */
79 return 0;
80 #elif defined(CONFIG_IA64)
81 /*
82 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
83 */
84 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
85 #elif defined(CONFIG_PPC64)
86 /* On PPC64, we always do non-cacheable access to the IO hole and
87 * cacheable elsewhere. Cache paradox can checkstop the CPU and
88 * the high_memory heuristic below is wrong on machines with memory
89 * above the IO hole... Ah, and of course, XFree86 doesn't pass
90 * O_SYNC when mapping us to tap IO space. Surprised ?
91 */
92 return !page_is_ram(addr >> PAGE_SHIFT);
93 #else
94 /*
95 * Accessing memory above the top the kernel knows about or through a file pointer
96 * that was marked O_SYNC will be done non-cached.
97 */
98 if (file->f_flags & O_SYNC)
99 return 1;
100 return addr >= __pa(high_memory);
101 #endif
102 }
104 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
105 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
106 {
107 unsigned long end_mem;
109 end_mem = __pa(high_memory);
110 if (addr >= end_mem)
111 return 0;
113 if (*count > end_mem - addr)
114 *count = end_mem - addr;
116 return 1;
117 }
118 #endif
120 static ssize_t do_write_mem(void *p, unsigned long realp,
121 const char __user * buf, size_t count, loff_t *ppos)
122 {
123 ssize_t written;
124 unsigned long copied;
126 written = 0;
127 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
128 /* we don't have page 0 mapped on sparc and m68k.. */
129 if (realp < PAGE_SIZE) {
130 unsigned long sz = PAGE_SIZE-realp;
131 if (sz > count) sz = count;
132 /* Hmm. Do something? */
133 buf+=sz;
134 p+=sz;
135 count-=sz;
136 written+=sz;
137 }
138 #endif
139 copied = copy_from_user(p, buf, count);
140 if (copied) {
141 ssize_t ret = written + (count - copied);
143 if (ret)
144 return ret;
145 return -EFAULT;
146 }
147 written += count;
148 *ppos += written;
149 return written;
150 }
153 /*
154 * This funcion reads the *physical* memory. The f_pos points directly to the
155 * memory location.
156 */
157 static ssize_t read_mem(struct file * file, char __user * buf,
158 size_t count, loff_t *ppos)
159 {
160 unsigned long p = *ppos;
161 ssize_t read;
163 if (!valid_phys_addr_range(p, &count))
164 return -EFAULT;
165 read = 0;
166 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
167 /* we don't have page 0 mapped on sparc and m68k.. */
168 if (p < PAGE_SIZE) {
169 unsigned long sz = PAGE_SIZE-p;
170 if (sz > count)
171 sz = count;
172 if (sz > 0) {
173 if (clear_user(buf, sz))
174 return -EFAULT;
175 buf += sz;
176 p += sz;
177 count -= sz;
178 read += sz;
179 }
180 }
181 #endif
182 if (copy_to_user(buf, __va(p), count))
183 return -EFAULT;
184 read += count;
185 *ppos += read;
186 return read;
187 }
189 static ssize_t write_mem(struct file * file, const char __user * buf,
190 size_t count, loff_t *ppos)
191 {
192 unsigned long p = *ppos;
194 if (!valid_phys_addr_range(p, &count))
195 return -EFAULT;
196 return do_write_mem(__va(p), p, buf, count, ppos);
197 }
199 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
200 {
201 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
202 int uncached;
204 uncached = uncached_access(file, offset);
205 #ifdef pgprot_noncached
206 if (uncached)
207 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
208 #endif
210 /* Don't try to swap out physical pages.. */
211 vma->vm_flags |= VM_RESERVED;
213 /*
214 * Don't dump addresses that are not real memory to a core file.
215 */
216 if (uncached)
217 vma->vm_flags |= VM_IO;
219 #if defined(CONFIG_XEN)
220 if (io_remap_page_range(vma, vma->vm_start, offset,
221 vma->vm_end-vma->vm_start, vma->vm_page_prot))
222 return -EAGAIN;
223 #else
224 if (remap_page_range(vma, vma->vm_start, offset, vma->vm_end-vma->vm_start,
225 vma->vm_page_prot))
226 return -EAGAIN;
227 #endif
228 return 0;
229 }
231 extern long vread(char *buf, char *addr, unsigned long count);
232 extern long vwrite(char *buf, char *addr, unsigned long count);
234 /*
235 * This function reads the *virtual* memory as seen by the kernel.
236 */
237 static ssize_t read_kmem(struct file *file, char __user *buf,
238 size_t count, loff_t *ppos)
239 {
240 unsigned long p = *ppos;
241 ssize_t read = 0;
242 ssize_t virtr = 0;
243 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
245 if (p < (unsigned long) high_memory) {
246 read = count;
247 if (count > (unsigned long) high_memory - p)
248 read = (unsigned long) high_memory - p;
250 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
251 /* we don't have page 0 mapped on sparc and m68k.. */
252 if (p < PAGE_SIZE && read > 0) {
253 size_t tmp = PAGE_SIZE - p;
254 if (tmp > read) tmp = read;
255 if (clear_user(buf, tmp))
256 return -EFAULT;
257 buf += tmp;
258 p += tmp;
259 read -= tmp;
260 count -= tmp;
261 }
262 #endif
263 if (copy_to_user(buf, (char *)p, read))
264 return -EFAULT;
265 p += read;
266 buf += read;
267 count -= read;
268 }
270 if (count > 0) {
271 kbuf = (char *)__get_free_page(GFP_KERNEL);
272 if (!kbuf)
273 return -ENOMEM;
274 while (count > 0) {
275 int len = count;
277 if (len > PAGE_SIZE)
278 len = PAGE_SIZE;
279 len = vread(kbuf, (char *)p, len);
280 if (!len)
281 break;
282 if (copy_to_user(buf, kbuf, len)) {
283 free_page((unsigned long)kbuf);
284 return -EFAULT;
285 }
286 count -= len;
287 buf += len;
288 virtr += len;
289 p += len;
290 }
291 free_page((unsigned long)kbuf);
292 }
293 *ppos = p;
294 return virtr + read;
295 }
297 /*
298 * This function writes to the *virtual* memory as seen by the kernel.
299 */
300 static ssize_t write_kmem(struct file * file, const char __user * buf,
301 size_t count, loff_t *ppos)
302 {
303 unsigned long p = *ppos;
304 ssize_t wrote = 0;
305 ssize_t virtr = 0;
306 ssize_t written;
307 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
309 if (p < (unsigned long) high_memory) {
311 wrote = count;
312 if (count > (unsigned long) high_memory - p)
313 wrote = (unsigned long) high_memory - p;
315 written = do_write_mem((void*)p, p, buf, wrote, ppos);
316 if (written != wrote)
317 return written;
318 wrote = written;
319 p += wrote;
320 buf += wrote;
321 count -= wrote;
322 }
324 if (count > 0) {
325 kbuf = (char *)__get_free_page(GFP_KERNEL);
326 if (!kbuf)
327 return wrote ? wrote : -ENOMEM;
328 while (count > 0) {
329 int len = count;
331 if (len > PAGE_SIZE)
332 len = PAGE_SIZE;
333 if (len) {
334 written = copy_from_user(kbuf, buf, len);
335 if (written) {
336 ssize_t ret;
338 free_page((unsigned long)kbuf);
339 ret = wrote + virtr + (len - written);
340 return ret ? ret : -EFAULT;
341 }
342 }
343 len = vwrite(kbuf, (char *)p, len);
344 count -= len;
345 buf += len;
346 virtr += len;
347 p += len;
348 }
349 free_page((unsigned long)kbuf);
350 }
352 *ppos = p;
353 return virtr + wrote;
354 }
356 #if defined(CONFIG_ISA) || !defined(__mc68000__)
357 static ssize_t read_port(struct file * file, char __user * buf,
358 size_t count, loff_t *ppos)
359 {
360 unsigned long i = *ppos;
361 char __user *tmp = buf;
363 if (verify_area(VERIFY_WRITE,buf,count))
364 return -EFAULT;
365 while (count-- > 0 && i < 65536) {
366 if (__put_user(inb(i),tmp) < 0)
367 return -EFAULT;
368 i++;
369 tmp++;
370 }
371 *ppos = i;
372 return tmp-buf;
373 }
375 static ssize_t write_port(struct file * file, const char __user * buf,
376 size_t count, loff_t *ppos)
377 {
378 unsigned long i = *ppos;
379 const char __user * tmp = buf;
381 if (verify_area(VERIFY_READ,buf,count))
382 return -EFAULT;
383 while (count-- > 0 && i < 65536) {
384 char c;
385 if (__get_user(c, tmp))
386 return -EFAULT;
387 outb(c,i);
388 i++;
389 tmp++;
390 }
391 *ppos = i;
392 return tmp-buf;
393 }
394 #endif
396 static ssize_t read_null(struct file * file, char __user * buf,
397 size_t count, loff_t *ppos)
398 {
399 return 0;
400 }
402 static ssize_t write_null(struct file * file, const char __user * buf,
403 size_t count, loff_t *ppos)
404 {
405 return count;
406 }
408 #ifdef CONFIG_MMU
409 /*
410 * For fun, we are using the MMU for this.
411 */
412 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
413 {
414 struct mm_struct *mm;
415 struct vm_area_struct * vma;
416 unsigned long addr=(unsigned long)buf;
418 mm = current->mm;
419 /* Oops, this was forgotten before. -ben */
420 down_read(&mm->mmap_sem);
422 /* For private mappings, just map in zero pages. */
423 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
424 unsigned long count;
426 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
427 goto out_up;
428 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
429 break;
430 count = vma->vm_end - addr;
431 if (count > size)
432 count = size;
434 zap_page_range(vma, addr, count, NULL);
435 zeromap_page_range(vma, addr, count, PAGE_COPY);
437 size -= count;
438 buf += count;
439 addr += count;
440 if (size == 0)
441 goto out_up;
442 }
444 up_read(&mm->mmap_sem);
446 /* The shared case is hard. Let's do the conventional zeroing. */
447 do {
448 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
449 if (unwritten)
450 return size + unwritten - PAGE_SIZE;
451 cond_resched();
452 buf += PAGE_SIZE;
453 size -= PAGE_SIZE;
454 } while (size);
456 return size;
457 out_up:
458 up_read(&mm->mmap_sem);
459 return size;
460 }
462 static ssize_t read_zero(struct file * file, char __user * buf,
463 size_t count, loff_t *ppos)
464 {
465 unsigned long left, unwritten, written = 0;
467 if (!count)
468 return 0;
470 if (!access_ok(VERIFY_WRITE, buf, count))
471 return -EFAULT;
473 left = count;
475 /* do we want to be clever? Arbitrary cut-off */
476 if (count >= PAGE_SIZE*4) {
477 unsigned long partial;
479 /* How much left of the page? */
480 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
481 unwritten = clear_user(buf, partial);
482 written = partial - unwritten;
483 if (unwritten)
484 goto out;
485 left -= partial;
486 buf += partial;
487 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
488 written += (left & PAGE_MASK) - unwritten;
489 if (unwritten)
490 goto out;
491 buf += left & PAGE_MASK;
492 left &= ~PAGE_MASK;
493 }
494 unwritten = clear_user(buf, left);
495 written += left - unwritten;
496 out:
497 return written ? written : -EFAULT;
498 }
500 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
501 {
502 if (vma->vm_flags & VM_SHARED)
503 return shmem_zero_setup(vma);
504 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
505 return -EAGAIN;
506 return 0;
507 }
508 #else /* CONFIG_MMU */
509 static ssize_t read_zero(struct file * file, char * buf,
510 size_t count, loff_t *ppos)
511 {
512 size_t todo = count;
514 while (todo) {
515 size_t chunk = todo;
517 if (chunk > 4096)
518 chunk = 4096; /* Just for latency reasons */
519 if (clear_user(buf, chunk))
520 return -EFAULT;
521 buf += chunk;
522 todo -= chunk;
523 cond_resched();
524 }
525 return count;
526 }
528 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
529 {
530 return -ENOSYS;
531 }
532 #endif /* CONFIG_MMU */
534 static ssize_t write_full(struct file * file, const char __user * buf,
535 size_t count, loff_t *ppos)
536 {
537 return -ENOSPC;
538 }
540 /*
541 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
542 * can fopen() both devices with "a" now. This was previously impossible.
543 * -- SRB.
544 */
546 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
547 {
548 return file->f_pos = 0;
549 }
551 /*
552 * The memory devices use the full 32/64 bits of the offset, and so we cannot
553 * check against negative addresses: they are ok. The return value is weird,
554 * though, in that case (0).
555 *
556 * also note that seeking relative to the "end of file" isn't supported:
557 * it has no meaning, so it returns -EINVAL.
558 */
559 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
560 {
561 loff_t ret;
563 down(&file->f_dentry->d_inode->i_sem);
564 switch (orig) {
565 case 0:
566 file->f_pos = offset;
567 ret = file->f_pos;
568 force_successful_syscall_return();
569 break;
570 case 1:
571 file->f_pos += offset;
572 ret = file->f_pos;
573 force_successful_syscall_return();
574 break;
575 default:
576 ret = -EINVAL;
577 }
578 up(&file->f_dentry->d_inode->i_sem);
579 return ret;
580 }
582 static int open_port(struct inode * inode, struct file * filp)
583 {
584 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
585 }
587 #define mmap_kmem mmap_mem
588 #define zero_lseek null_lseek
589 #define full_lseek null_lseek
590 #define write_zero write_null
591 #define read_full read_zero
592 #define open_mem open_port
593 #define open_kmem open_mem
595 static struct file_operations mem_fops = {
596 .llseek = memory_lseek,
597 .read = read_mem,
598 .write = write_mem,
599 .mmap = mmap_mem,
600 .open = open_mem,
601 };
603 static struct file_operations kmem_fops = {
604 .llseek = memory_lseek,
605 .read = read_kmem,
606 .write = write_kmem,
607 .mmap = mmap_kmem,
608 .open = open_kmem,
609 };
611 static struct file_operations null_fops = {
612 .llseek = null_lseek,
613 .read = read_null,
614 .write = write_null,
615 };
617 #if defined(CONFIG_ISA) || !defined(__mc68000__)
618 static struct file_operations port_fops = {
619 .llseek = memory_lseek,
620 .read = read_port,
621 .write = write_port,
622 .open = open_port,
623 };
624 #endif
626 static struct file_operations zero_fops = {
627 .llseek = zero_lseek,
628 .read = read_zero,
629 .write = write_zero,
630 .mmap = mmap_zero,
631 };
633 static struct file_operations full_fops = {
634 .llseek = full_lseek,
635 .read = read_full,
636 .write = write_full,
637 };
639 static ssize_t kmsg_write(struct file * file, const char __user * buf,
640 size_t count, loff_t *ppos)
641 {
642 char *tmp;
643 int ret;
645 tmp = kmalloc(count + 1, GFP_KERNEL);
646 if (tmp == NULL)
647 return -ENOMEM;
648 ret = -EFAULT;
649 if (!copy_from_user(tmp, buf, count)) {
650 tmp[count] = 0;
651 ret = printk("%s", tmp);
652 }
653 kfree(tmp);
654 return ret;
655 }
657 static struct file_operations kmsg_fops = {
658 .write = kmsg_write,
659 };
661 static int memory_open(struct inode * inode, struct file * filp)
662 {
663 switch (iminor(inode)) {
664 case 1:
665 filp->f_op = &mem_fops;
666 break;
667 case 2:
668 filp->f_op = &kmem_fops;
669 break;
670 case 3:
671 filp->f_op = &null_fops;
672 break;
673 #if defined(CONFIG_ISA) || !defined(__mc68000__)
674 case 4:
675 filp->f_op = &port_fops;
676 break;
677 #endif
678 case 5:
679 filp->f_op = &zero_fops;
680 break;
681 case 7:
682 filp->f_op = &full_fops;
683 break;
684 case 8:
685 filp->f_op = &random_fops;
686 break;
687 case 9:
688 filp->f_op = &urandom_fops;
689 break;
690 case 11:
691 filp->f_op = &kmsg_fops;
692 break;
693 default:
694 return -ENXIO;
695 }
696 if (filp->f_op && filp->f_op->open)
697 return filp->f_op->open(inode,filp);
698 return 0;
699 }
701 static struct file_operations memory_fops = {
702 .open = memory_open, /* just a selector for the real open */
703 };
705 static const struct {
706 unsigned int minor;
707 char *name;
708 umode_t mode;
709 struct file_operations *fops;
710 } devlist[] = { /* list of minor devices */
711 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
712 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
713 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
714 #if defined(CONFIG_ISA) || !defined(__mc68000__)
715 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
716 #endif
717 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
718 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
719 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
720 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
721 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
722 };
724 static struct class_simple *mem_class;
726 static int __init chr_dev_init(void)
727 {
728 int i;
730 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
731 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
733 mem_class = class_simple_create(THIS_MODULE, "mem");
734 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
735 class_simple_device_add(mem_class,
736 MKDEV(MEM_MAJOR, devlist[i].minor),
737 NULL, devlist[i].name);
738 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
739 S_IFCHR | devlist[i].mode, devlist[i].name);
740 }
742 return 0;
743 }
745 fs_initcall(chr_dev_init);