debuggers.hg

view linux-2.6.10-rc2-xen-sparse/drivers/char/mem.c @ 3289:a169836882cb

bitkeeper revision 1.1159.170.59 (41b4c2fdJ2gj_BWy27Vj3ptayZp_yg)

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