ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/char/mem.c @ 9296:f85bb99187bf

Update interface documentation to include sched_op_new hypercall
and clean up the style a bit. Also clean up the sched_op_new
description in the sched.h public header.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Mar 15 19:19:22 2006 +0100 (2006-03-15)
parents 2494b4e00cbb
children 4fad820a2233
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>
26 #include <linux/highmem.h>
27 #include <linux/crash_dump.h>
28 #include <linux/backing-dev.h>
29 #include <linux/bootmem.h>
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
34 #ifdef CONFIG_IA64
35 # include <linux/efi.h>
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 #else
80 /*
81 * Accessing memory above the top the kernel knows about or through a file pointer
82 * that was marked O_SYNC will be done non-cached.
83 */
84 if (file->f_flags & O_SYNC)
85 return 1;
86 return addr >= __pa(high_memory);
87 #endif
88 }
90 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
91 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
92 {
93 unsigned long end_mem;
95 end_mem = __pa(high_memory);
96 if (addr >= end_mem)
97 return 0;
99 if (*count > end_mem - addr)
100 *count = end_mem - addr;
102 return 1;
103 }
105 static inline int valid_mmap_phys_addr_range(unsigned long addr, size_t *size)
106 {
107 return 1;
108 }
109 #endif
111 #ifndef ARCH_HAS_DEV_MEM
112 /*
113 * This funcion reads the *physical* memory. The f_pos points directly to the
114 * memory location.
115 */
116 static ssize_t read_mem(struct file * file, char __user * buf,
117 size_t count, loff_t *ppos)
118 {
119 unsigned long p = *ppos;
120 ssize_t read, sz;
121 char *ptr;
123 if (!valid_phys_addr_range(p, &count))
124 return -EFAULT;
125 read = 0;
126 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
127 /* we don't have page 0 mapped on sparc and m68k.. */
128 if (p < PAGE_SIZE) {
129 sz = PAGE_SIZE - p;
130 if (sz > count)
131 sz = count;
132 if (sz > 0) {
133 if (clear_user(buf, sz))
134 return -EFAULT;
135 buf += sz;
136 p += sz;
137 count -= sz;
138 read += sz;
139 }
140 }
141 #endif
143 while (count > 0) {
144 /*
145 * Handle first page in case it's not aligned
146 */
147 if (-p & (PAGE_SIZE - 1))
148 sz = -p & (PAGE_SIZE - 1);
149 else
150 sz = PAGE_SIZE;
152 sz = min_t(unsigned long, sz, count);
154 /*
155 * On ia64 if a page has been mapped somewhere as
156 * uncached, then it must also be accessed uncached
157 * by the kernel or data corruption may occur
158 */
159 ptr = xlate_dev_mem_ptr(p);
161 if (copy_to_user(buf, ptr, sz))
162 return -EFAULT;
163 buf += sz;
164 p += sz;
165 count -= sz;
166 read += sz;
167 }
169 *ppos += read;
170 return read;
171 }
173 static ssize_t write_mem(struct file * file, const char __user * buf,
174 size_t count, loff_t *ppos)
175 {
176 unsigned long p = *ppos;
177 ssize_t written, sz;
178 unsigned long copied;
179 void *ptr;
181 if (!valid_phys_addr_range(p, &count))
182 return -EFAULT;
184 written = 0;
186 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
187 /* we don't have page 0 mapped on sparc and m68k.. */
188 if (p < PAGE_SIZE) {
189 unsigned long sz = PAGE_SIZE - p;
190 if (sz > count)
191 sz = count;
192 /* Hmm. Do something? */
193 buf += sz;
194 p += sz;
195 count -= sz;
196 written += sz;
197 }
198 #endif
200 while (count > 0) {
201 /*
202 * Handle first page in case it's not aligned
203 */
204 if (-p & (PAGE_SIZE - 1))
205 sz = -p & (PAGE_SIZE - 1);
206 else
207 sz = PAGE_SIZE;
209 sz = min_t(unsigned long, sz, count);
211 /*
212 * On ia64 if a page has been mapped somewhere as
213 * uncached, then it must also be accessed uncached
214 * by the kernel or data corruption may occur
215 */
216 ptr = xlate_dev_mem_ptr(p);
218 copied = copy_from_user(ptr, buf, sz);
219 if (copied) {
220 ssize_t ret;
222 ret = written + (sz - copied);
223 if (ret)
224 return ret;
225 return -EFAULT;
226 }
227 buf += sz;
228 p += sz;
229 count -= sz;
230 written += sz;
231 }
233 *ppos += written;
234 return written;
235 }
236 #endif
238 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
239 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
240 unsigned long size, pgprot_t vma_prot)
241 {
242 #ifdef pgprot_noncached
243 unsigned long offset = pfn << PAGE_SHIFT;
245 if (uncached_access(file, offset))
246 return pgprot_noncached(vma_prot);
247 #endif
248 return vma_prot;
249 }
250 #endif
252 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
253 {
254 size_t size = vma->vm_end - vma->vm_start;
256 if (!valid_mmap_phys_addr_range(vma->vm_pgoff << PAGE_SHIFT, &size))
257 return -EINVAL;
259 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
260 size,
261 vma->vm_page_prot);
263 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
264 if (remap_pfn_range(vma,
265 vma->vm_start,
266 vma->vm_pgoff,
267 size,
268 vma->vm_page_prot))
269 return -EAGAIN;
270 return 0;
271 }
273 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
274 {
275 unsigned long pfn;
277 /* Turn a kernel-virtual address into a physical page frame */
278 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
280 /*
281 * RED-PEN: on some architectures there is more mapped memory
282 * than available in mem_map which pfn_valid checks
283 * for. Perhaps should add a new macro here.
284 *
285 * RED-PEN: vmalloc is not supported right now.
286 */
287 if (!pfn_valid(pfn))
288 return -EIO;
290 vma->vm_pgoff = pfn;
291 return mmap_mem(file, vma);
292 }
294 #ifdef CONFIG_CRASH_DUMP
295 /*
296 * Read memory corresponding to the old kernel.
297 */
298 static ssize_t read_oldmem(struct file *file, char __user *buf,
299 size_t count, loff_t *ppos)
300 {
301 unsigned long pfn, offset;
302 size_t read = 0, csize;
303 int rc = 0;
305 while (count) {
306 pfn = *ppos / PAGE_SIZE;
307 if (pfn > saved_max_pfn)
308 return read;
310 offset = (unsigned long)(*ppos % PAGE_SIZE);
311 if (count > PAGE_SIZE - offset)
312 csize = PAGE_SIZE - offset;
313 else
314 csize = count;
316 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
317 if (rc < 0)
318 return rc;
319 buf += csize;
320 *ppos += csize;
321 read += csize;
322 count -= csize;
323 }
324 return read;
325 }
326 #endif
328 extern long vread(char *buf, char *addr, unsigned long count);
329 extern long vwrite(char *buf, char *addr, unsigned long count);
331 /*
332 * This function reads the *virtual* memory as seen by the kernel.
333 */
334 static ssize_t read_kmem(struct file *file, char __user *buf,
335 size_t count, loff_t *ppos)
336 {
337 unsigned long p = *ppos;
338 ssize_t low_count, read, sz;
339 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
341 read = 0;
342 if (p < (unsigned long) high_memory) {
343 low_count = count;
344 if (count > (unsigned long) high_memory - p)
345 low_count = (unsigned long) high_memory - p;
347 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
348 /* we don't have page 0 mapped on sparc and m68k.. */
349 if (p < PAGE_SIZE && low_count > 0) {
350 size_t tmp = PAGE_SIZE - p;
351 if (tmp > low_count) tmp = low_count;
352 if (clear_user(buf, tmp))
353 return -EFAULT;
354 buf += tmp;
355 p += tmp;
356 read += tmp;
357 low_count -= tmp;
358 count -= tmp;
359 }
360 #endif
361 while (low_count > 0) {
362 /*
363 * Handle first page in case it's not aligned
364 */
365 if (-p & (PAGE_SIZE - 1))
366 sz = -p & (PAGE_SIZE - 1);
367 else
368 sz = PAGE_SIZE;
370 sz = min_t(unsigned long, sz, low_count);
372 /*
373 * On ia64 if a page has been mapped somewhere as
374 * uncached, then it must also be accessed uncached
375 * by the kernel or data corruption may occur
376 */
377 kbuf = xlate_dev_kmem_ptr((char *)p);
379 if (copy_to_user(buf, kbuf, sz))
380 return -EFAULT;
381 buf += sz;
382 p += sz;
383 read += sz;
384 low_count -= sz;
385 count -= sz;
386 }
387 }
389 if (count > 0) {
390 kbuf = (char *)__get_free_page(GFP_KERNEL);
391 if (!kbuf)
392 return -ENOMEM;
393 while (count > 0) {
394 int len = count;
396 if (len > PAGE_SIZE)
397 len = PAGE_SIZE;
398 len = vread(kbuf, (char *)p, len);
399 if (!len)
400 break;
401 if (copy_to_user(buf, kbuf, len)) {
402 free_page((unsigned long)kbuf);
403 return -EFAULT;
404 }
405 count -= len;
406 buf += len;
407 read += len;
408 p += len;
409 }
410 free_page((unsigned long)kbuf);
411 }
412 *ppos = p;
413 return read;
414 }
417 static inline ssize_t
418 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
419 size_t count, loff_t *ppos)
420 {
421 ssize_t written, sz;
422 unsigned long copied;
424 written = 0;
425 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
426 /* we don't have page 0 mapped on sparc and m68k.. */
427 if (realp < PAGE_SIZE) {
428 unsigned long sz = PAGE_SIZE - realp;
429 if (sz > count)
430 sz = count;
431 /* Hmm. Do something? */
432 buf += sz;
433 p += sz;
434 realp += sz;
435 count -= sz;
436 written += sz;
437 }
438 #endif
440 while (count > 0) {
441 char *ptr;
442 /*
443 * Handle first page in case it's not aligned
444 */
445 if (-realp & (PAGE_SIZE - 1))
446 sz = -realp & (PAGE_SIZE - 1);
447 else
448 sz = PAGE_SIZE;
450 sz = min_t(unsigned long, sz, count);
452 /*
453 * On ia64 if a page has been mapped somewhere as
454 * uncached, then it must also be accessed uncached
455 * by the kernel or data corruption may occur
456 */
457 ptr = xlate_dev_kmem_ptr(p);
459 copied = copy_from_user(ptr, buf, sz);
460 if (copied) {
461 ssize_t ret;
463 ret = written + (sz - copied);
464 if (ret)
465 return ret;
466 return -EFAULT;
467 }
468 buf += sz;
469 p += sz;
470 realp += sz;
471 count -= sz;
472 written += sz;
473 }
475 *ppos += written;
476 return written;
477 }
480 /*
481 * This function writes to the *virtual* memory as seen by the kernel.
482 */
483 static ssize_t write_kmem(struct file * file, const char __user * buf,
484 size_t count, loff_t *ppos)
485 {
486 unsigned long p = *ppos;
487 ssize_t wrote = 0;
488 ssize_t virtr = 0;
489 ssize_t written;
490 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
492 if (p < (unsigned long) high_memory) {
494 wrote = count;
495 if (count > (unsigned long) high_memory - p)
496 wrote = (unsigned long) high_memory - p;
498 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
499 if (written != wrote)
500 return written;
501 wrote = written;
502 p += wrote;
503 buf += wrote;
504 count -= wrote;
505 }
507 if (count > 0) {
508 kbuf = (char *)__get_free_page(GFP_KERNEL);
509 if (!kbuf)
510 return wrote ? wrote : -ENOMEM;
511 while (count > 0) {
512 int len = count;
514 if (len > PAGE_SIZE)
515 len = PAGE_SIZE;
516 if (len) {
517 written = copy_from_user(kbuf, buf, len);
518 if (written) {
519 ssize_t ret;
521 free_page((unsigned long)kbuf);
522 ret = wrote + virtr + (len - written);
523 return ret ? ret : -EFAULT;
524 }
525 }
526 len = vwrite(kbuf, (char *)p, len);
527 count -= len;
528 buf += len;
529 virtr += len;
530 p += len;
531 }
532 free_page((unsigned long)kbuf);
533 }
535 *ppos = p;
536 return virtr + wrote;
537 }
539 #if defined(CONFIG_ISA) || !defined(__mc68000__)
540 static ssize_t read_port(struct file * file, char __user * buf,
541 size_t count, loff_t *ppos)
542 {
543 unsigned long i = *ppos;
544 char __user *tmp = buf;
546 if (!access_ok(VERIFY_WRITE, buf, count))
547 return -EFAULT;
548 while (count-- > 0 && i < 65536) {
549 if (__put_user(inb(i),tmp) < 0)
550 return -EFAULT;
551 i++;
552 tmp++;
553 }
554 *ppos = i;
555 return tmp-buf;
556 }
558 static ssize_t write_port(struct file * file, const char __user * buf,
559 size_t count, loff_t *ppos)
560 {
561 unsigned long i = *ppos;
562 const char __user * tmp = buf;
564 if (!access_ok(VERIFY_READ,buf,count))
565 return -EFAULT;
566 while (count-- > 0 && i < 65536) {
567 char c;
568 if (__get_user(c, tmp))
569 return -EFAULT;
570 outb(c,i);
571 i++;
572 tmp++;
573 }
574 *ppos = i;
575 return tmp-buf;
576 }
577 #endif
579 static ssize_t read_null(struct file * file, char __user * buf,
580 size_t count, loff_t *ppos)
581 {
582 return 0;
583 }
585 static ssize_t write_null(struct file * file, const char __user * buf,
586 size_t count, loff_t *ppos)
587 {
588 return count;
589 }
591 #ifdef CONFIG_MMU
592 /*
593 * For fun, we are using the MMU for this.
594 */
595 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
596 {
597 struct mm_struct *mm;
598 struct vm_area_struct * vma;
599 unsigned long addr=(unsigned long)buf;
601 mm = current->mm;
602 /* Oops, this was forgotten before. -ben */
603 down_read(&mm->mmap_sem);
605 /* For private mappings, just map in zero pages. */
606 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
607 unsigned long count;
609 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
610 goto out_up;
611 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
612 break;
613 count = vma->vm_end - addr;
614 if (count > size)
615 count = size;
617 zap_page_range(vma, addr, count, NULL);
618 zeromap_page_range(vma, addr, count, PAGE_COPY);
620 size -= count;
621 buf += count;
622 addr += count;
623 if (size == 0)
624 goto out_up;
625 }
627 up_read(&mm->mmap_sem);
629 /* The shared case is hard. Let's do the conventional zeroing. */
630 do {
631 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
632 if (unwritten)
633 return size + unwritten - PAGE_SIZE;
634 cond_resched();
635 buf += PAGE_SIZE;
636 size -= PAGE_SIZE;
637 } while (size);
639 return size;
640 out_up:
641 up_read(&mm->mmap_sem);
642 return size;
643 }
645 static ssize_t read_zero(struct file * file, char __user * buf,
646 size_t count, loff_t *ppos)
647 {
648 unsigned long left, unwritten, written = 0;
650 if (!count)
651 return 0;
653 if (!access_ok(VERIFY_WRITE, buf, count))
654 return -EFAULT;
656 left = count;
658 /* do we want to be clever? Arbitrary cut-off */
659 if (count >= PAGE_SIZE*4) {
660 unsigned long partial;
662 /* How much left of the page? */
663 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
664 unwritten = clear_user(buf, partial);
665 written = partial - unwritten;
666 if (unwritten)
667 goto out;
668 left -= partial;
669 buf += partial;
670 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
671 written += (left & PAGE_MASK) - unwritten;
672 if (unwritten)
673 goto out;
674 buf += left & PAGE_MASK;
675 left &= ~PAGE_MASK;
676 }
677 unwritten = clear_user(buf, left);
678 written += left - unwritten;
679 out:
680 return written ? written : -EFAULT;
681 }
683 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
684 {
685 if (vma->vm_flags & VM_SHARED)
686 return shmem_zero_setup(vma);
687 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
688 return -EAGAIN;
689 return 0;
690 }
691 #else /* CONFIG_MMU */
692 static ssize_t read_zero(struct file * file, char * buf,
693 size_t count, loff_t *ppos)
694 {
695 size_t todo = count;
697 while (todo) {
698 size_t chunk = todo;
700 if (chunk > 4096)
701 chunk = 4096; /* Just for latency reasons */
702 if (clear_user(buf, chunk))
703 return -EFAULT;
704 buf += chunk;
705 todo -= chunk;
706 cond_resched();
707 }
708 return count;
709 }
711 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
712 {
713 return -ENOSYS;
714 }
715 #endif /* CONFIG_MMU */
717 static ssize_t write_full(struct file * file, const char __user * buf,
718 size_t count, loff_t *ppos)
719 {
720 return -ENOSPC;
721 }
723 /*
724 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
725 * can fopen() both devices with "a" now. This was previously impossible.
726 * -- SRB.
727 */
729 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
730 {
731 return file->f_pos = 0;
732 }
734 /*
735 * The memory devices use the full 32/64 bits of the offset, and so we cannot
736 * check against negative addresses: they are ok. The return value is weird,
737 * though, in that case (0).
738 *
739 * also note that seeking relative to the "end of file" isn't supported:
740 * it has no meaning, so it returns -EINVAL.
741 */
742 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
743 {
744 loff_t ret;
746 mutex_lock(&file->f_dentry->d_inode->i_mutex);
747 switch (orig) {
748 case 0:
749 file->f_pos = offset;
750 ret = file->f_pos;
751 force_successful_syscall_return();
752 break;
753 case 1:
754 file->f_pos += offset;
755 ret = file->f_pos;
756 force_successful_syscall_return();
757 break;
758 default:
759 ret = -EINVAL;
760 }
761 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
762 return ret;
763 }
765 static int open_port(struct inode * inode, struct file * filp)
766 {
767 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
768 }
770 #define zero_lseek null_lseek
771 #define full_lseek null_lseek
772 #define write_zero write_null
773 #define read_full read_zero
774 #define open_mem open_port
775 #define open_kmem open_mem
776 #define open_oldmem open_mem
778 #ifndef ARCH_HAS_DEV_MEM
779 static struct file_operations mem_fops = {
780 .llseek = memory_lseek,
781 .read = read_mem,
782 .write = write_mem,
783 .mmap = mmap_mem,
784 .open = open_mem,
785 };
786 #else
787 extern struct file_operations mem_fops;
788 #endif
790 static struct file_operations kmem_fops = {
791 .llseek = memory_lseek,
792 .read = read_kmem,
793 .write = write_kmem,
794 .mmap = mmap_kmem,
795 .open = open_kmem,
796 };
798 static struct file_operations null_fops = {
799 .llseek = null_lseek,
800 .read = read_null,
801 .write = write_null,
802 };
804 #if defined(CONFIG_ISA) || !defined(__mc68000__)
805 static struct file_operations port_fops = {
806 .llseek = memory_lseek,
807 .read = read_port,
808 .write = write_port,
809 .open = open_port,
810 };
811 #endif
813 static struct file_operations zero_fops = {
814 .llseek = zero_lseek,
815 .read = read_zero,
816 .write = write_zero,
817 .mmap = mmap_zero,
818 };
820 static struct backing_dev_info zero_bdi = {
821 .capabilities = BDI_CAP_MAP_COPY,
822 };
824 static struct file_operations full_fops = {
825 .llseek = full_lseek,
826 .read = read_full,
827 .write = write_full,
828 };
830 #ifdef CONFIG_CRASH_DUMP
831 static struct file_operations oldmem_fops = {
832 .read = read_oldmem,
833 .open = open_oldmem,
834 };
835 #endif
837 static ssize_t kmsg_write(struct file * file, const char __user * buf,
838 size_t count, loff_t *ppos)
839 {
840 char *tmp;
841 ssize_t ret;
843 tmp = kmalloc(count + 1, GFP_KERNEL);
844 if (tmp == NULL)
845 return -ENOMEM;
846 ret = -EFAULT;
847 if (!copy_from_user(tmp, buf, count)) {
848 tmp[count] = 0;
849 ret = printk("%s", tmp);
850 if (ret > count)
851 /* printk can add a prefix */
852 ret = count;
853 }
854 kfree(tmp);
855 return ret;
856 }
858 static struct file_operations kmsg_fops = {
859 .write = kmsg_write,
860 };
862 static int memory_open(struct inode * inode, struct file * filp)
863 {
864 switch (iminor(inode)) {
865 case 1:
866 filp->f_op = &mem_fops;
867 break;
868 case 2:
869 filp->f_op = &kmem_fops;
870 break;
871 case 3:
872 filp->f_op = &null_fops;
873 break;
874 #if defined(CONFIG_ISA) || !defined(__mc68000__)
875 case 4:
876 filp->f_op = &port_fops;
877 break;
878 #endif
879 case 5:
880 filp->f_mapping->backing_dev_info = &zero_bdi;
881 filp->f_op = &zero_fops;
882 break;
883 case 7:
884 filp->f_op = &full_fops;
885 break;
886 case 8:
887 filp->f_op = &random_fops;
888 break;
889 case 9:
890 filp->f_op = &urandom_fops;
891 break;
892 case 11:
893 filp->f_op = &kmsg_fops;
894 break;
895 #ifdef CONFIG_CRASH_DUMP
896 case 12:
897 filp->f_op = &oldmem_fops;
898 break;
899 #endif
900 default:
901 return -ENXIO;
902 }
903 if (filp->f_op && filp->f_op->open)
904 return filp->f_op->open(inode,filp);
905 return 0;
906 }
908 static struct file_operations memory_fops = {
909 .open = memory_open, /* just a selector for the real open */
910 };
912 static const struct {
913 unsigned int minor;
914 char *name;
915 umode_t mode;
916 struct file_operations *fops;
917 } devlist[] = { /* list of minor devices */
918 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
919 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
920 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
921 #if defined(CONFIG_ISA) || !defined(__mc68000__)
922 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
923 #endif
924 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
925 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
926 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
927 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
928 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
929 #ifdef CONFIG_CRASH_DUMP
930 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
931 #endif
932 };
934 static struct class *mem_class;
936 static int __init chr_dev_init(void)
937 {
938 int i;
940 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
941 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
943 mem_class = class_create(THIS_MODULE, "mem");
944 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
945 class_device_create(mem_class, NULL,
946 MKDEV(MEM_MAJOR, devlist[i].minor),
947 NULL, devlist[i].name);
948 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
949 S_IFCHR | devlist[i].mode, devlist[i].name);
950 }
952 return 0;
953 }
955 fs_initcall(chr_dev_init);