debuggers.hg

view linux-2.6.8.1-xen-sparse/drivers/char/mem.c @ 2625:0f3e0ef73bd5

bitkeeper revision 1.1159.98.2 (415e927er1yu43w4cm3_dj36NPzRJA)

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