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