blob: 91dd669273e0018aeeb95b02e18b4549e074a661 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Vivek Goyal50b1fdb2005-06-25 14:58:23 -070026#include <linux/highmem.h>
27#include <linux/crash_dump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/backing-dev.h>
Vivek Goyal315c2152005-06-25 14:58:24 -070029#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/uaccess.h>
32#include <asm/io.h>
33
34#ifdef CONFIG_IA64
35# include <linux/efi.h>
36#endif
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * Architectures vary in how they handle caching for addresses
40 * outside of main memory.
41 *
42 */
43static 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}
89
90#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
91static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
92{
93 unsigned long end_mem;
94
95 end_mem = __pa(high_memory);
96 if (addr >= end_mem)
97 return 0;
98
99 if (*count > end_mem - addr)
100 *count = end_mem - addr;
101
102 return 1;
103}
104#endif
105
106/*
107 * This funcion reads the *physical* memory. The f_pos points directly to the
108 * memory location.
109 */
110static ssize_t read_mem(struct file * file, char __user * buf,
111 size_t count, loff_t *ppos)
112{
113 unsigned long p = *ppos;
114 ssize_t read, sz;
115 char *ptr;
116
117 if (!valid_phys_addr_range(p, &count))
118 return -EFAULT;
119 read = 0;
120#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
121 /* we don't have page 0 mapped on sparc and m68k.. */
122 if (p < PAGE_SIZE) {
123 sz = PAGE_SIZE - p;
124 if (sz > count)
125 sz = count;
126 if (sz > 0) {
127 if (clear_user(buf, sz))
128 return -EFAULT;
129 buf += sz;
130 p += sz;
131 count -= sz;
132 read += sz;
133 }
134 }
135#endif
136
137 while (count > 0) {
138 /*
139 * Handle first page in case it's not aligned
140 */
141 if (-p & (PAGE_SIZE - 1))
142 sz = -p & (PAGE_SIZE - 1);
143 else
144 sz = PAGE_SIZE;
145
146 sz = min_t(unsigned long, sz, count);
147
148 /*
149 * On ia64 if a page has been mapped somewhere as
150 * uncached, then it must also be accessed uncached
151 * by the kernel or data corruption may occur
152 */
153 ptr = xlate_dev_mem_ptr(p);
154
155 if (copy_to_user(buf, ptr, sz))
156 return -EFAULT;
157 buf += sz;
158 p += sz;
159 count -= sz;
160 read += sz;
161 }
162
163 *ppos += read;
164 return read;
165}
166
167static ssize_t write_mem(struct file * file, const char __user * buf,
168 size_t count, loff_t *ppos)
169{
170 unsigned long p = *ppos;
171 ssize_t written, sz;
172 unsigned long copied;
173 void *ptr;
174
175 if (!valid_phys_addr_range(p, &count))
176 return -EFAULT;
177
178 written = 0;
179
180#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
181 /* we don't have page 0 mapped on sparc and m68k.. */
182 if (p < PAGE_SIZE) {
183 unsigned long sz = PAGE_SIZE - p;
184 if (sz > count)
185 sz = count;
186 /* Hmm. Do something? */
187 buf += sz;
188 p += sz;
189 count -= sz;
190 written += sz;
191 }
192#endif
193
194 while (count > 0) {
195 /*
196 * Handle first page in case it's not aligned
197 */
198 if (-p & (PAGE_SIZE - 1))
199 sz = -p & (PAGE_SIZE - 1);
200 else
201 sz = PAGE_SIZE;
202
203 sz = min_t(unsigned long, sz, count);
204
205 /*
206 * On ia64 if a page has been mapped somewhere as
207 * uncached, then it must also be accessed uncached
208 * by the kernel or data corruption may occur
209 */
210 ptr = xlate_dev_mem_ptr(p);
211
212 copied = copy_from_user(ptr, buf, sz);
213 if (copied) {
214 ssize_t ret;
215
216 ret = written + (sz - copied);
217 if (ret)
218 return ret;
219 return -EFAULT;
220 }
221 buf += sz;
222 p += sz;
223 count -= sz;
224 written += sz;
225 }
226
227 *ppos += written;
228 return written;
229}
230
231static int mmap_mem(struct file * file, struct vm_area_struct * vma)
232{
233#if defined(__HAVE_PHYS_MEM_ACCESS_PROT)
Roland Dreier8b150472005-10-28 17:46:18 -0700234 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 vma->vm_end - vma->vm_start,
236 vma->vm_page_prot);
237#elif defined(pgprot_noncached)
238 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
239 int uncached;
240
241 uncached = uncached_access(file, offset);
242 if (uncached)
243 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
244#endif
245
246 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
247 if (remap_pfn_range(vma,
248 vma->vm_start,
249 vma->vm_pgoff,
250 vma->vm_end-vma->vm_start,
251 vma->vm_page_prot))
252 return -EAGAIN;
253 return 0;
254}
255
256static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
257{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700258 unsigned long pfn;
259
260 /* Turn a kernel-virtual address into a physical page frame */
261 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /*
264 * RED-PEN: on some architectures there is more mapped memory
265 * than available in mem_map which pfn_valid checks
266 * for. Perhaps should add a new macro here.
267 *
268 * RED-PEN: vmalloc is not supported right now.
269 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700270 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700272
273 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return mmap_mem(file, vma);
275}
276
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700277#ifdef CONFIG_CRASH_DUMP
278/*
279 * Read memory corresponding to the old kernel.
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700280 */
Vivek Goyal315c2152005-06-25 14:58:24 -0700281static ssize_t read_oldmem(struct file *file, char __user *buf,
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700282 size_t count, loff_t *ppos)
283{
Vivek Goyal315c2152005-06-25 14:58:24 -0700284 unsigned long pfn, offset;
285 size_t read = 0, csize;
286 int rc = 0;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700287
Maneesh Soni72414d32005-06-25 14:58:28 -0700288 while (count) {
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700289 pfn = *ppos / PAGE_SIZE;
Vivek Goyal315c2152005-06-25 14:58:24 -0700290 if (pfn > saved_max_pfn)
291 return read;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700292
Vivek Goyal315c2152005-06-25 14:58:24 -0700293 offset = (unsigned long)(*ppos % PAGE_SIZE);
294 if (count > PAGE_SIZE - offset)
295 csize = PAGE_SIZE - offset;
296 else
297 csize = count;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700298
Vivek Goyal315c2152005-06-25 14:58:24 -0700299 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
300 if (rc < 0)
301 return rc;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700302 buf += csize;
303 *ppos += csize;
304 read += csize;
305 count -= csize;
306 }
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700307 return read;
308}
309#endif
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311extern long vread(char *buf, char *addr, unsigned long count);
312extern long vwrite(char *buf, char *addr, unsigned long count);
313
314/*
315 * This function reads the *virtual* memory as seen by the kernel.
316 */
317static ssize_t read_kmem(struct file *file, char __user *buf,
318 size_t count, loff_t *ppos)
319{
320 unsigned long p = *ppos;
321 ssize_t low_count, read, sz;
322 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
323
324 read = 0;
325 if (p < (unsigned long) high_memory) {
326 low_count = count;
327 if (count > (unsigned long) high_memory - p)
328 low_count = (unsigned long) high_memory - p;
329
330#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
331 /* we don't have page 0 mapped on sparc and m68k.. */
332 if (p < PAGE_SIZE && low_count > 0) {
333 size_t tmp = PAGE_SIZE - p;
334 if (tmp > low_count) tmp = low_count;
335 if (clear_user(buf, tmp))
336 return -EFAULT;
337 buf += tmp;
338 p += tmp;
339 read += tmp;
340 low_count -= tmp;
341 count -= tmp;
342 }
343#endif
344 while (low_count > 0) {
345 /*
346 * Handle first page in case it's not aligned
347 */
348 if (-p & (PAGE_SIZE - 1))
349 sz = -p & (PAGE_SIZE - 1);
350 else
351 sz = PAGE_SIZE;
352
353 sz = min_t(unsigned long, sz, low_count);
354
355 /*
356 * On ia64 if a page has been mapped somewhere as
357 * uncached, then it must also be accessed uncached
358 * by the kernel or data corruption may occur
359 */
360 kbuf = xlate_dev_kmem_ptr((char *)p);
361
362 if (copy_to_user(buf, kbuf, sz))
363 return -EFAULT;
364 buf += sz;
365 p += sz;
366 read += sz;
367 low_count -= sz;
368 count -= sz;
369 }
370 }
371
372 if (count > 0) {
373 kbuf = (char *)__get_free_page(GFP_KERNEL);
374 if (!kbuf)
375 return -ENOMEM;
376 while (count > 0) {
377 int len = count;
378
379 if (len > PAGE_SIZE)
380 len = PAGE_SIZE;
381 len = vread(kbuf, (char *)p, len);
382 if (!len)
383 break;
384 if (copy_to_user(buf, kbuf, len)) {
385 free_page((unsigned long)kbuf);
386 return -EFAULT;
387 }
388 count -= len;
389 buf += len;
390 read += len;
391 p += len;
392 }
393 free_page((unsigned long)kbuf);
394 }
395 *ppos = p;
396 return read;
397}
398
399
400static inline ssize_t
401do_write_kmem(void *p, unsigned long realp, const char __user * buf,
402 size_t count, loff_t *ppos)
403{
404 ssize_t written, sz;
405 unsigned long copied;
406
407 written = 0;
408#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
409 /* we don't have page 0 mapped on sparc and m68k.. */
410 if (realp < PAGE_SIZE) {
411 unsigned long sz = PAGE_SIZE - realp;
412 if (sz > count)
413 sz = count;
414 /* Hmm. Do something? */
415 buf += sz;
416 p += sz;
417 realp += sz;
418 count -= sz;
419 written += sz;
420 }
421#endif
422
423 while (count > 0) {
424 char *ptr;
425 /*
426 * Handle first page in case it's not aligned
427 */
428 if (-realp & (PAGE_SIZE - 1))
429 sz = -realp & (PAGE_SIZE - 1);
430 else
431 sz = PAGE_SIZE;
432
433 sz = min_t(unsigned long, sz, count);
434
435 /*
436 * On ia64 if a page has been mapped somewhere as
437 * uncached, then it must also be accessed uncached
438 * by the kernel or data corruption may occur
439 */
440 ptr = xlate_dev_kmem_ptr(p);
441
442 copied = copy_from_user(ptr, buf, sz);
443 if (copied) {
444 ssize_t ret;
445
446 ret = written + (sz - copied);
447 if (ret)
448 return ret;
449 return -EFAULT;
450 }
451 buf += sz;
452 p += sz;
453 realp += sz;
454 count -= sz;
455 written += sz;
456 }
457
458 *ppos += written;
459 return written;
460}
461
462
463/*
464 * This function writes to the *virtual* memory as seen by the kernel.
465 */
466static ssize_t write_kmem(struct file * file, const char __user * buf,
467 size_t count, loff_t *ppos)
468{
469 unsigned long p = *ppos;
470 ssize_t wrote = 0;
471 ssize_t virtr = 0;
472 ssize_t written;
473 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
474
475 if (p < (unsigned long) high_memory) {
476
477 wrote = count;
478 if (count > (unsigned long) high_memory - p)
479 wrote = (unsigned long) high_memory - p;
480
481 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
482 if (written != wrote)
483 return written;
484 wrote = written;
485 p += wrote;
486 buf += wrote;
487 count -= wrote;
488 }
489
490 if (count > 0) {
491 kbuf = (char *)__get_free_page(GFP_KERNEL);
492 if (!kbuf)
493 return wrote ? wrote : -ENOMEM;
494 while (count > 0) {
495 int len = count;
496
497 if (len > PAGE_SIZE)
498 len = PAGE_SIZE;
499 if (len) {
500 written = copy_from_user(kbuf, buf, len);
501 if (written) {
502 ssize_t ret;
503
504 free_page((unsigned long)kbuf);
505 ret = wrote + virtr + (len - written);
506 return ret ? ret : -EFAULT;
507 }
508 }
509 len = vwrite(kbuf, (char *)p, len);
510 count -= len;
511 buf += len;
512 virtr += len;
513 p += len;
514 }
515 free_page((unsigned long)kbuf);
516 }
517
518 *ppos = p;
519 return virtr + wrote;
520}
521
Stephen Rothwell145d01e2005-06-21 17:15:52 -0700522#if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static ssize_t read_port(struct file * file, char __user * buf,
524 size_t count, loff_t *ppos)
525{
526 unsigned long i = *ppos;
527 char __user *tmp = buf;
528
529 if (!access_ok(VERIFY_WRITE, buf, count))
530 return -EFAULT;
531 while (count-- > 0 && i < 65536) {
532 if (__put_user(inb(i),tmp) < 0)
533 return -EFAULT;
534 i++;
535 tmp++;
536 }
537 *ppos = i;
538 return tmp-buf;
539}
540
541static ssize_t write_port(struct file * file, const char __user * buf,
542 size_t count, loff_t *ppos)
543{
544 unsigned long i = *ppos;
545 const char __user * tmp = buf;
546
547 if (!access_ok(VERIFY_READ,buf,count))
548 return -EFAULT;
549 while (count-- > 0 && i < 65536) {
550 char c;
551 if (__get_user(c, tmp))
552 return -EFAULT;
553 outb(c,i);
554 i++;
555 tmp++;
556 }
557 *ppos = i;
558 return tmp-buf;
559}
560#endif
561
562static ssize_t read_null(struct file * file, char __user * buf,
563 size_t count, loff_t *ppos)
564{
565 return 0;
566}
567
568static ssize_t write_null(struct file * file, const char __user * buf,
569 size_t count, loff_t *ppos)
570{
571 return count;
572}
573
574#ifdef CONFIG_MMU
575/*
576 * For fun, we are using the MMU for this.
577 */
578static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
579{
580 struct mm_struct *mm;
581 struct vm_area_struct * vma;
582 unsigned long addr=(unsigned long)buf;
583
584 mm = current->mm;
585 /* Oops, this was forgotten before. -ben */
586 down_read(&mm->mmap_sem);
587
588 /* For private mappings, just map in zero pages. */
589 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
590 unsigned long count;
591
592 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
593 goto out_up;
Linus Torvalds6aab3412005-11-28 14:34:23 -0800594 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 count = vma->vm_end - addr;
597 if (count > size)
598 count = size;
599
600 zap_page_range(vma, addr, count, NULL);
601 zeromap_page_range(vma, addr, count, PAGE_COPY);
602
603 size -= count;
604 buf += count;
605 addr += count;
606 if (size == 0)
607 goto out_up;
608 }
609
610 up_read(&mm->mmap_sem);
611
612 /* The shared case is hard. Let's do the conventional zeroing. */
613 do {
614 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
615 if (unwritten)
616 return size + unwritten - PAGE_SIZE;
617 cond_resched();
618 buf += PAGE_SIZE;
619 size -= PAGE_SIZE;
620 } while (size);
621
622 return size;
623out_up:
624 up_read(&mm->mmap_sem);
625 return size;
626}
627
628static ssize_t read_zero(struct file * file, char __user * buf,
629 size_t count, loff_t *ppos)
630{
631 unsigned long left, unwritten, written = 0;
632
633 if (!count)
634 return 0;
635
636 if (!access_ok(VERIFY_WRITE, buf, count))
637 return -EFAULT;
638
639 left = count;
640
641 /* do we want to be clever? Arbitrary cut-off */
642 if (count >= PAGE_SIZE*4) {
643 unsigned long partial;
644
645 /* How much left of the page? */
646 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
647 unwritten = clear_user(buf, partial);
648 written = partial - unwritten;
649 if (unwritten)
650 goto out;
651 left -= partial;
652 buf += partial;
653 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
654 written += (left & PAGE_MASK) - unwritten;
655 if (unwritten)
656 goto out;
657 buf += left & PAGE_MASK;
658 left &= ~PAGE_MASK;
659 }
660 unwritten = clear_user(buf, left);
661 written += left - unwritten;
662out:
663 return written ? written : -EFAULT;
664}
665
666static int mmap_zero(struct file * file, struct vm_area_struct * vma)
667{
668 if (vma->vm_flags & VM_SHARED)
669 return shmem_zero_setup(vma);
670 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
671 return -EAGAIN;
672 return 0;
673}
674#else /* CONFIG_MMU */
675static ssize_t read_zero(struct file * file, char * buf,
676 size_t count, loff_t *ppos)
677{
678 size_t todo = count;
679
680 while (todo) {
681 size_t chunk = todo;
682
683 if (chunk > 4096)
684 chunk = 4096; /* Just for latency reasons */
685 if (clear_user(buf, chunk))
686 return -EFAULT;
687 buf += chunk;
688 todo -= chunk;
689 cond_resched();
690 }
691 return count;
692}
693
694static int mmap_zero(struct file * file, struct vm_area_struct * vma)
695{
696 return -ENOSYS;
697}
698#endif /* CONFIG_MMU */
699
700static ssize_t write_full(struct file * file, const char __user * buf,
701 size_t count, loff_t *ppos)
702{
703 return -ENOSPC;
704}
705
706/*
707 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
708 * can fopen() both devices with "a" now. This was previously impossible.
709 * -- SRB.
710 */
711
712static loff_t null_lseek(struct file * file, loff_t offset, int orig)
713{
714 return file->f_pos = 0;
715}
716
717/*
718 * The memory devices use the full 32/64 bits of the offset, and so we cannot
719 * check against negative addresses: they are ok. The return value is weird,
720 * though, in that case (0).
721 *
722 * also note that seeking relative to the "end of file" isn't supported:
723 * it has no meaning, so it returns -EINVAL.
724 */
725static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
726{
727 loff_t ret;
728
729 down(&file->f_dentry->d_inode->i_sem);
730 switch (orig) {
731 case 0:
732 file->f_pos = offset;
733 ret = file->f_pos;
734 force_successful_syscall_return();
735 break;
736 case 1:
737 file->f_pos += offset;
738 ret = file->f_pos;
739 force_successful_syscall_return();
740 break;
741 default:
742 ret = -EINVAL;
743 }
744 up(&file->f_dentry->d_inode->i_sem);
745 return ret;
746}
747
748static int open_port(struct inode * inode, struct file * filp)
749{
750 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
751}
752
753#define zero_lseek null_lseek
754#define full_lseek null_lseek
755#define write_zero write_null
756#define read_full read_zero
757#define open_mem open_port
758#define open_kmem open_mem
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700759#define open_oldmem open_mem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761static struct file_operations mem_fops = {
762 .llseek = memory_lseek,
763 .read = read_mem,
764 .write = write_mem,
765 .mmap = mmap_mem,
766 .open = open_mem,
767};
768
769static struct file_operations kmem_fops = {
770 .llseek = memory_lseek,
771 .read = read_kmem,
772 .write = write_kmem,
773 .mmap = mmap_kmem,
774 .open = open_kmem,
775};
776
777static struct file_operations null_fops = {
778 .llseek = null_lseek,
779 .read = read_null,
780 .write = write_null,
781};
782
Stephen Rothwell145d01e2005-06-21 17:15:52 -0700783#if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784static struct file_operations port_fops = {
785 .llseek = memory_lseek,
786 .read = read_port,
787 .write = write_port,
788 .open = open_port,
789};
790#endif
791
792static struct file_operations zero_fops = {
793 .llseek = zero_lseek,
794 .read = read_zero,
795 .write = write_zero,
796 .mmap = mmap_zero,
797};
798
799static struct backing_dev_info zero_bdi = {
800 .capabilities = BDI_CAP_MAP_COPY,
801};
802
803static struct file_operations full_fops = {
804 .llseek = full_lseek,
805 .read = read_full,
806 .write = write_full,
807};
808
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700809#ifdef CONFIG_CRASH_DUMP
810static struct file_operations oldmem_fops = {
811 .read = read_oldmem,
812 .open = open_oldmem,
813};
814#endif
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static ssize_t kmsg_write(struct file * file, const char __user * buf,
817 size_t count, loff_t *ppos)
818{
819 char *tmp;
820 int ret;
821
822 tmp = kmalloc(count + 1, GFP_KERNEL);
823 if (tmp == NULL)
824 return -ENOMEM;
825 ret = -EFAULT;
826 if (!copy_from_user(tmp, buf, count)) {
827 tmp[count] = 0;
828 ret = printk("%s", tmp);
829 }
830 kfree(tmp);
831 return ret;
832}
833
834static struct file_operations kmsg_fops = {
835 .write = kmsg_write,
836};
837
838static int memory_open(struct inode * inode, struct file * filp)
839{
840 switch (iminor(inode)) {
841 case 1:
842 filp->f_op = &mem_fops;
843 break;
844 case 2:
845 filp->f_op = &kmem_fops;
846 break;
847 case 3:
848 filp->f_op = &null_fops;
849 break;
Stephen Rothwell145d01e2005-06-21 17:15:52 -0700850#if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 case 4:
852 filp->f_op = &port_fops;
853 break;
854#endif
855 case 5:
856 filp->f_mapping->backing_dev_info = &zero_bdi;
857 filp->f_op = &zero_fops;
858 break;
859 case 7:
860 filp->f_op = &full_fops;
861 break;
862 case 8:
863 filp->f_op = &random_fops;
864 break;
865 case 9:
866 filp->f_op = &urandom_fops;
867 break;
868 case 11:
869 filp->f_op = &kmsg_fops;
870 break;
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700871#ifdef CONFIG_CRASH_DUMP
872 case 12:
873 filp->f_op = &oldmem_fops;
874 break;
875#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 default:
877 return -ENXIO;
878 }
879 if (filp->f_op && filp->f_op->open)
880 return filp->f_op->open(inode,filp);
881 return 0;
882}
883
884static struct file_operations memory_fops = {
885 .open = memory_open, /* just a selector for the real open */
886};
887
888static const struct {
889 unsigned int minor;
890 char *name;
891 umode_t mode;
892 struct file_operations *fops;
893} devlist[] = { /* list of minor devices */
894 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
895 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
896 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
Stephen Rothwell145d01e2005-06-21 17:15:52 -0700897#if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
899#endif
900 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
901 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
902 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
903 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
904 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
Vivek Goyal50b1fdb2005-06-25 14:58:23 -0700905#ifdef CONFIG_CRASH_DUMP
906 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908};
909
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800910static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912static int __init chr_dev_init(void)
913{
914 int i;
915
916 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
917 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
918
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800919 mem_class = class_create(THIS_MODULE, "mem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700921 class_device_create(mem_class, NULL,
922 MKDEV(MEM_MAJOR, devlist[i].minor),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 NULL, devlist[i].name);
924 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
925 S_IFCHR | devlist[i].mode, devlist[i].name);
926 }
927
928 return 0;
929}
930
931fs_initcall(chr_dev_init);