2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8 * Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
11 #include <linux/config.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>
35 # include <linux/efi.h>
38 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
39 extern void tapechar_init(void);
43 * Architectures vary in how they handle caching for addresses
44 * outside of main memory.
47 static inline int uncached_access(struct file
*file
, unsigned long addr
)
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.
58 if (file
->f_flags
& O_SYNC
)
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__)
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.
72 if (file
->f_flags
& O_SYNC
)
74 /* same behaviour as i386. PAT always set to cached and MTRRs control the
76 Hopefully a full PAT implementation will fix that soon. */
78 #elif defined(CONFIG_IA64)
80 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
82 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
85 * Accessing memory above the top the kernel knows about or through a file pointer
86 * that was marked O_SYNC will be done non-cached.
88 if (file
->f_flags
& O_SYNC
)
90 return addr
>= __pa(high_memory
);
94 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
95 static inline int valid_phys_addr_range(unsigned long addr
, size_t *count
)
97 unsigned long end_mem
;
99 end_mem
= __pa(high_memory
);
103 if (*count
> end_mem
- addr
)
104 *count
= end_mem
- addr
;
111 * This funcion reads the *physical* memory. The f_pos points directly to the
114 static ssize_t
read_mem(struct file
* file
, char __user
* buf
,
115 size_t count
, loff_t
*ppos
)
117 unsigned long p
= *ppos
;
121 if (!valid_phys_addr_range(p
, &count
))
124 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
125 /* we don't have page 0 mapped on sparc and m68k.. */
131 if (clear_user(buf
, sz
))
143 * Handle first page in case it's not aligned
145 if (-p
& (PAGE_SIZE
- 1))
146 sz
= -p
& (PAGE_SIZE
- 1);
150 sz
= min_t(unsigned long, sz
, count
);
153 * On ia64 if a page has been mapped somewhere as
154 * uncached, then it must also be accessed uncached
155 * by the kernel or data corruption may occur
157 ptr
= xlate_dev_mem_ptr(p
);
159 if (copy_to_user(buf
, ptr
, sz
))
171 static ssize_t
write_mem(struct file
* file
, const char __user
* buf
,
172 size_t count
, loff_t
*ppos
)
174 unsigned long p
= *ppos
;
176 unsigned long copied
;
179 if (!valid_phys_addr_range(p
, &count
))
184 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
185 /* we don't have page 0 mapped on sparc and m68k.. */
187 unsigned long sz
= PAGE_SIZE
- p
;
190 /* Hmm. Do something? */
200 * Handle first page in case it's not aligned
202 if (-p
& (PAGE_SIZE
- 1))
203 sz
= -p
& (PAGE_SIZE
- 1);
207 sz
= min_t(unsigned long, sz
, count
);
210 * On ia64 if a page has been mapped somewhere as
211 * uncached, then it must also be accessed uncached
212 * by the kernel or data corruption may occur
214 ptr
= xlate_dev_mem_ptr(p
);
216 copied
= copy_from_user(ptr
, buf
, sz
);
220 ret
= written
+ (sz
- copied
);
235 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
237 #if defined(__HAVE_PHYS_MEM_ACCESS_PROT)
238 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
240 vma
->vm_page_prot
= phys_mem_access_prot(file
, offset
,
241 vma
->vm_end
- vma
->vm_start
,
243 #elif defined(pgprot_noncached)
244 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
247 uncached
= uncached_access(file
, offset
);
249 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
252 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
253 if (remap_pfn_range(vma
,
256 vma
->vm_end
-vma
->vm_start
,
262 static int mmap_kmem(struct file
* file
, struct vm_area_struct
* vma
)
264 unsigned long long val
;
266 * RED-PEN: on some architectures there is more mapped memory
267 * than available in mem_map which pfn_valid checks
268 * for. Perhaps should add a new macro here.
270 * RED-PEN: vmalloc is not supported right now.
272 if (!pfn_valid(vma
->vm_pgoff
))
274 val
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
;
275 vma
->vm_pgoff
= __pa(val
) >> PAGE_SHIFT
;
276 return mmap_mem(file
, vma
);
279 #ifdef CONFIG_CRASH_DUMP
281 * Read memory corresponding to the old kernel.
283 static ssize_t
read_oldmem(struct file
*file
, char __user
*buf
,
284 size_t count
, loff_t
*ppos
)
286 unsigned long pfn
, offset
;
287 size_t read
= 0, csize
;
291 pfn
= *ppos
/ PAGE_SIZE
;
292 if (pfn
> saved_max_pfn
)
295 offset
= (unsigned long)(*ppos
% PAGE_SIZE
);
296 if (count
> PAGE_SIZE
- offset
)
297 csize
= PAGE_SIZE
- offset
;
301 rc
= copy_oldmem_page(pfn
, buf
, csize
, offset
, 1);
313 extern long vread(char *buf
, char *addr
, unsigned long count
);
314 extern long vwrite(char *buf
, char *addr
, unsigned long count
);
317 * This function reads the *virtual* memory as seen by the kernel.
319 static ssize_t
read_kmem(struct file
*file
, char __user
*buf
,
320 size_t count
, loff_t
*ppos
)
322 unsigned long p
= *ppos
;
323 ssize_t low_count
, read
, sz
;
324 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
327 if (p
< (unsigned long) high_memory
) {
329 if (count
> (unsigned long) high_memory
- p
)
330 low_count
= (unsigned long) high_memory
- p
;
332 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
333 /* we don't have page 0 mapped on sparc and m68k.. */
334 if (p
< PAGE_SIZE
&& low_count
> 0) {
335 size_t tmp
= PAGE_SIZE
- p
;
336 if (tmp
> low_count
) tmp
= low_count
;
337 if (clear_user(buf
, tmp
))
346 while (low_count
> 0) {
348 * Handle first page in case it's not aligned
350 if (-p
& (PAGE_SIZE
- 1))
351 sz
= -p
& (PAGE_SIZE
- 1);
355 sz
= min_t(unsigned long, sz
, low_count
);
358 * On ia64 if a page has been mapped somewhere as
359 * uncached, then it must also be accessed uncached
360 * by the kernel or data corruption may occur
362 kbuf
= xlate_dev_kmem_ptr((char *)p
);
364 if (copy_to_user(buf
, kbuf
, sz
))
375 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
383 len
= vread(kbuf
, (char *)p
, len
);
386 if (copy_to_user(buf
, kbuf
, len
)) {
387 free_page((unsigned long)kbuf
);
395 free_page((unsigned long)kbuf
);
402 static inline ssize_t
403 do_write_kmem(void *p
, unsigned long realp
, const char __user
* buf
,
404 size_t count
, loff_t
*ppos
)
407 unsigned long copied
;
410 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
411 /* we don't have page 0 mapped on sparc and m68k.. */
412 if (realp
< PAGE_SIZE
) {
413 unsigned long sz
= PAGE_SIZE
- realp
;
416 /* Hmm. Do something? */
428 * Handle first page in case it's not aligned
430 if (-realp
& (PAGE_SIZE
- 1))
431 sz
= -realp
& (PAGE_SIZE
- 1);
435 sz
= min_t(unsigned long, sz
, count
);
438 * On ia64 if a page has been mapped somewhere as
439 * uncached, then it must also be accessed uncached
440 * by the kernel or data corruption may occur
442 ptr
= xlate_dev_kmem_ptr(p
);
444 copied
= copy_from_user(ptr
, buf
, sz
);
448 ret
= written
+ (sz
- copied
);
466 * This function writes to the *virtual* memory as seen by the kernel.
468 static ssize_t
write_kmem(struct file
* file
, const char __user
* buf
,
469 size_t count
, loff_t
*ppos
)
471 unsigned long p
= *ppos
;
475 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
477 if (p
< (unsigned long) high_memory
) {
480 if (count
> (unsigned long) high_memory
- p
)
481 wrote
= (unsigned long) high_memory
- p
;
483 written
= do_write_kmem((void*)p
, p
, buf
, wrote
, ppos
);
484 if (written
!= wrote
)
493 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
495 return wrote
? wrote
: -ENOMEM
;
502 written
= copy_from_user(kbuf
, buf
, len
);
506 free_page((unsigned long)kbuf
);
507 ret
= wrote
+ virtr
+ (len
- written
);
508 return ret
? ret
: -EFAULT
;
511 len
= vwrite(kbuf
, (char *)p
, len
);
517 free_page((unsigned long)kbuf
);
521 return virtr
+ wrote
;
524 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
525 static ssize_t
read_port(struct file
* file
, char __user
* buf
,
526 size_t count
, loff_t
*ppos
)
528 unsigned long i
= *ppos
;
529 char __user
*tmp
= buf
;
531 if (!access_ok(VERIFY_WRITE
, buf
, count
))
533 while (count
-- > 0 && i
< 65536) {
534 if (__put_user(inb(i
),tmp
) < 0)
543 static ssize_t
write_port(struct file
* file
, const char __user
* buf
,
544 size_t count
, loff_t
*ppos
)
546 unsigned long i
= *ppos
;
547 const char __user
* tmp
= buf
;
549 if (!access_ok(VERIFY_READ
,buf
,count
))
551 while (count
-- > 0 && i
< 65536) {
553 if (__get_user(c
, tmp
))
564 static ssize_t
read_null(struct file
* file
, char __user
* buf
,
565 size_t count
, loff_t
*ppos
)
570 static ssize_t
write_null(struct file
* file
, const char __user
* buf
,
571 size_t count
, loff_t
*ppos
)
578 * For fun, we are using the MMU for this.
580 static inline size_t read_zero_pagealigned(char __user
* buf
, size_t size
)
582 struct mm_struct
*mm
;
583 struct vm_area_struct
* vma
;
584 unsigned long addr
=(unsigned long)buf
;
587 /* Oops, this was forgotten before. -ben */
588 down_read(&mm
->mmap_sem
);
590 /* For private mappings, just map in zero pages. */
591 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
594 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
596 if (vma
->vm_flags
& (VM_SHARED
| VM_HUGETLB
))
598 count
= vma
->vm_end
- addr
;
602 zap_page_range(vma
, addr
, count
, NULL
);
603 zeromap_page_range(vma
, addr
, count
, PAGE_COPY
);
612 up_read(&mm
->mmap_sem
);
614 /* The shared case is hard. Let's do the conventional zeroing. */
616 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
618 return size
+ unwritten
- PAGE_SIZE
;
626 up_read(&mm
->mmap_sem
);
630 static ssize_t
read_zero(struct file
* file
, char __user
* buf
,
631 size_t count
, loff_t
*ppos
)
633 unsigned long left
, unwritten
, written
= 0;
638 if (!access_ok(VERIFY_WRITE
, buf
, count
))
643 /* do we want to be clever? Arbitrary cut-off */
644 if (count
>= PAGE_SIZE
*4) {
645 unsigned long partial
;
647 /* How much left of the page? */
648 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
649 unwritten
= clear_user(buf
, partial
);
650 written
= partial
- unwritten
;
655 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
656 written
+= (left
& PAGE_MASK
) - unwritten
;
659 buf
+= left
& PAGE_MASK
;
662 unwritten
= clear_user(buf
, left
);
663 written
+= left
- unwritten
;
665 return written
? written
: -EFAULT
;
668 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
670 if (vma
->vm_flags
& VM_SHARED
)
671 return shmem_zero_setup(vma
);
672 if (zeromap_page_range(vma
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
676 #else /* CONFIG_MMU */
677 static ssize_t
read_zero(struct file
* file
, char * buf
,
678 size_t count
, loff_t
*ppos
)
686 chunk
= 4096; /* Just for latency reasons */
687 if (clear_user(buf
, chunk
))
696 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
700 #endif /* CONFIG_MMU */
702 static ssize_t
write_full(struct file
* file
, const char __user
* buf
,
703 size_t count
, loff_t
*ppos
)
709 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
710 * can fopen() both devices with "a" now. This was previously impossible.
714 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
716 return file
->f_pos
= 0;
720 * The memory devices use the full 32/64 bits of the offset, and so we cannot
721 * check against negative addresses: they are ok. The return value is weird,
722 * though, in that case (0).
724 * also note that seeking relative to the "end of file" isn't supported:
725 * it has no meaning, so it returns -EINVAL.
727 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
731 down(&file
->f_dentry
->d_inode
->i_sem
);
734 file
->f_pos
= offset
;
736 force_successful_syscall_return();
739 file
->f_pos
+= offset
;
741 force_successful_syscall_return();
746 up(&file
->f_dentry
->d_inode
->i_sem
);
750 static int open_port(struct inode
* inode
, struct file
* filp
)
752 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
755 #define zero_lseek null_lseek
756 #define full_lseek null_lseek
757 #define write_zero write_null
758 #define read_full read_zero
759 #define open_mem open_port
760 #define open_kmem open_mem
761 #define open_oldmem open_mem
763 static struct file_operations mem_fops
= {
764 .llseek
= memory_lseek
,
771 static struct file_operations kmem_fops
= {
772 .llseek
= memory_lseek
,
779 static struct file_operations null_fops
= {
780 .llseek
= null_lseek
,
785 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
786 static struct file_operations port_fops
= {
787 .llseek
= memory_lseek
,
794 static struct file_operations zero_fops
= {
795 .llseek
= zero_lseek
,
801 static struct backing_dev_info zero_bdi
= {
802 .capabilities
= BDI_CAP_MAP_COPY
,
805 static struct file_operations full_fops
= {
806 .llseek
= full_lseek
,
811 #ifdef CONFIG_CRASH_DUMP
812 static struct file_operations oldmem_fops
= {
818 static ssize_t
kmsg_write(struct file
* file
, const char __user
* buf
,
819 size_t count
, loff_t
*ppos
)
824 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
828 if (!copy_from_user(tmp
, buf
, count
)) {
830 ret
= printk("%s", tmp
);
836 static struct file_operations kmsg_fops
= {
840 static int memory_open(struct inode
* inode
, struct file
* filp
)
842 switch (iminor(inode
)) {
844 filp
->f_op
= &mem_fops
;
847 filp
->f_op
= &kmem_fops
;
850 filp
->f_op
= &null_fops
;
852 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
854 filp
->f_op
= &port_fops
;
858 filp
->f_mapping
->backing_dev_info
= &zero_bdi
;
859 filp
->f_op
= &zero_fops
;
862 filp
->f_op
= &full_fops
;
865 filp
->f_op
= &random_fops
;
868 filp
->f_op
= &urandom_fops
;
871 filp
->f_op
= &kmsg_fops
;
873 #ifdef CONFIG_CRASH_DUMP
875 filp
->f_op
= &oldmem_fops
;
881 if (filp
->f_op
&& filp
->f_op
->open
)
882 return filp
->f_op
->open(inode
,filp
);
886 static struct file_operations memory_fops
= {
887 .open
= memory_open
, /* just a selector for the real open */
890 static const struct {
894 struct file_operations
*fops
;
895 } devlist
[] = { /* list of minor devices */
896 {1, "mem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &mem_fops
},
897 {2, "kmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &kmem_fops
},
898 {3, "null", S_IRUGO
| S_IWUGO
, &null_fops
},
899 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
900 {4, "port", S_IRUSR
| S_IWUSR
| S_IRGRP
, &port_fops
},
902 {5, "zero", S_IRUGO
| S_IWUGO
, &zero_fops
},
903 {7, "full", S_IRUGO
| S_IWUGO
, &full_fops
},
904 {8, "random", S_IRUGO
| S_IWUSR
, &random_fops
},
905 {9, "urandom", S_IRUGO
| S_IWUSR
, &urandom_fops
},
906 {11,"kmsg", S_IRUGO
| S_IWUSR
, &kmsg_fops
},
907 #ifdef CONFIG_CRASH_DUMP
908 {12,"oldmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &oldmem_fops
},
912 static struct class *mem_class
;
914 static int __init
chr_dev_init(void)
918 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
919 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
921 mem_class
= class_create(THIS_MODULE
, "mem");
922 for (i
= 0; i
< ARRAY_SIZE(devlist
); i
++) {
923 class_device_create(mem_class
, MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
924 NULL
, devlist
[i
].name
);
925 devfs_mk_cdev(MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
926 S_IFCHR
| devlist
[i
].mode
, devlist
[i
].name
);
932 fs_initcall(chr_dev_init
);