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/tpqic02.h>
15 #include <linux/ftape.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mman.h>
19 #include <linux/random.h>
20 #include <linux/init.h>
21 #include <linux/raw.h>
22 #include <linux/tty.h>
23 #include <linux/capability.h>
24 #include <linux/smp_lock.h>
25 #include <linux/devfs_fs_kernel.h>
26 #include <linux/ptrace.h>
28 #include <asm/uaccess.h>
30 #include <asm/pgalloc.h>
33 # include <linux/efi.h>
37 extern void fbmem_init(void);
39 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
40 extern void tapechar_init(void);
44 * Architectures vary in how they handle caching for addresses
45 * outside of main memory.
48 static inline int uncached_access(struct file
*file
, unsigned long addr
)
52 * On the PPro and successors, the MTRRs are used to set
53 * memory types for physical addresses outside main memory,
54 * so blindly setting PCD or PWT on those pages is wrong.
55 * For Pentiums and earlier, the surround logic should disable
56 * caching for the high addresses through the KEN pin, but
57 * we maintain the tradition of paranoia in this code.
59 if (file
->f_flags
& O_SYNC
)
61 return !( test_bit(X86_FEATURE_MTRR
, boot_cpu_data
.x86_capability
) ||
62 test_bit(X86_FEATURE_K6_MTRR
, boot_cpu_data
.x86_capability
) ||
63 test_bit(X86_FEATURE_CYRIX_ARR
, boot_cpu_data
.x86_capability
) ||
64 test_bit(X86_FEATURE_CENTAUR_MCR
, boot_cpu_data
.x86_capability
) )
65 && addr
>= __pa(high_memory
);
66 #elif defined(CONFIG_IA64)
68 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
70 return !(efi_mem_attributes(addr
) & EFI_MEMORY_WB
);
73 * Accessing memory above the top the kernel knows about or through a file pointer
74 * that was marked O_SYNC will be done non-cached.
76 if (file
->f_flags
& O_SYNC
)
78 return addr
>= __pa(high_memory
);
82 static ssize_t
do_write_mem(struct file
* file
, void *p
, unsigned long realp
,
83 const char * buf
, size_t count
, loff_t
*ppos
)
88 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
89 /* we don't have page 0 mapped on sparc and m68k.. */
90 if (realp
< PAGE_SIZE
) {
91 unsigned long sz
= PAGE_SIZE
-realp
;
92 if (sz
> count
) sz
= count
;
93 /* Hmm. Do something? */
100 if (copy_from_user(p
, buf
, count
))
109 * This funcion reads the *physical* memory. The f_pos points directly to the
112 static ssize_t
read_mem(struct file
* file
, char * buf
,
113 size_t count
, loff_t
*ppos
)
115 unsigned long p
= *ppos
;
116 unsigned long end_mem
;
119 end_mem
= __pa(high_memory
);
122 if (count
> end_mem
- p
)
125 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
126 /* we don't have page 0 mapped on sparc and m68k.. */
128 unsigned long sz
= PAGE_SIZE
-p
;
132 if (clear_user(buf
, sz
))
141 if (copy_to_user(buf
, __va(p
), count
))
148 static ssize_t
write_mem(struct file
* file
, const char * buf
,
149 size_t count
, loff_t
*ppos
)
151 unsigned long p
= *ppos
;
152 unsigned long end_mem
;
154 end_mem
= __pa(high_memory
);
157 if (count
> end_mem
- p
)
159 return do_write_mem(file
, __va(p
), p
, buf
, count
, ppos
);
162 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
164 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
167 uncached
= uncached_access(file
, offset
);
168 #ifdef pgprot_noncached
170 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
173 /* Don't try to swap out physical pages.. */
174 vma
->vm_flags
|= VM_RESERVED
;
177 * Don't dump addresses that are not real memory to a core file.
180 vma
->vm_flags
|= VM_IO
;
182 if (remap_page_range(vma
, vma
->vm_start
, offset
, vma
->vm_end
-vma
->vm_start
,
188 extern long vread(char *buf
, char *addr
, unsigned long count
);
189 extern long vwrite(char *buf
, char *addr
, unsigned long count
);
192 * This function reads the *virtual* memory as seen by the kernel.
194 static ssize_t
read_kmem(struct file
*file
, char *buf
,
195 size_t count
, loff_t
*ppos
)
197 unsigned long p
= *ppos
;
200 char * kbuf
; /* k-addr because vread() takes vmlist_lock rwlock */
202 if (p
< (unsigned long) high_memory
) {
204 if (count
> (unsigned long) high_memory
- p
)
205 read
= (unsigned long) high_memory
- p
;
207 #if defined(__sparc__) || (defined(__mc68000__) && defined(CONFIG_MMU))
208 /* we don't have page 0 mapped on sparc and m68k.. */
209 if (p
< PAGE_SIZE
&& read
> 0) {
210 size_t tmp
= PAGE_SIZE
- p
;
211 if (tmp
> read
) tmp
= read
;
212 if (clear_user(buf
, tmp
))
220 if (copy_to_user(buf
, (char *)p
, read
))
228 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
236 len
= vread(kbuf
, (char *)p
, len
);
239 if (copy_to_user(buf
, kbuf
, len
)) {
240 free_page((unsigned long)kbuf
);
248 free_page((unsigned long)kbuf
);
255 * This function writes to the *virtual* memory as seen by the kernel.
257 static ssize_t
write_kmem(struct file
* file
, const char * buf
,
258 size_t count
, loff_t
*ppos
)
260 unsigned long p
= *ppos
;
263 char * kbuf
; /* k-addr because vwrite() takes vmlist_lock rwlock */
265 if (p
< (unsigned long) high_memory
) {
267 if (count
> (unsigned long) high_memory
- p
)
268 wrote
= (unsigned long) high_memory
- p
;
270 wrote
= do_write_mem(file
, (void*)p
, p
, buf
, wrote
, ppos
);
278 kbuf
= (char *)__get_free_page(GFP_KERNEL
);
286 if (len
&& copy_from_user(kbuf
, buf
, len
)) {
287 free_page((unsigned long)kbuf
);
290 len
= vwrite(kbuf
, (char *)p
, len
);
296 free_page((unsigned long)kbuf
);
300 return virtr
+ wrote
;
303 #if defined(CONFIG_ISA) || !defined(__mc68000__)
304 static ssize_t
read_port(struct file
* file
, char * buf
,
305 size_t count
, loff_t
*ppos
)
307 unsigned long i
= *ppos
;
310 if (verify_area(VERIFY_WRITE
,buf
,count
))
312 while (count
-- > 0 && i
< 65536) {
313 if (__put_user(inb(i
),tmp
) < 0)
322 static ssize_t
write_port(struct file
* file
, const char * buf
,
323 size_t count
, loff_t
*ppos
)
325 unsigned long i
= *ppos
;
326 const char * tmp
= buf
;
328 if (verify_area(VERIFY_READ
,buf
,count
))
330 while (count
-- > 0 && i
< 65536) {
332 if (__get_user(c
, tmp
))
343 static ssize_t
read_null(struct file
* file
, char * buf
,
344 size_t count
, loff_t
*ppos
)
349 static ssize_t
write_null(struct file
* file
, const char * buf
,
350 size_t count
, loff_t
*ppos
)
357 * For fun, we are using the MMU for this.
359 static inline size_t read_zero_pagealigned(char * buf
, size_t size
)
361 struct mm_struct
*mm
;
362 struct vm_area_struct
* vma
;
363 unsigned long addr
=(unsigned long)buf
;
366 /* Oops, this was forgotten before. -ben */
367 down_read(&mm
->mmap_sem
);
369 /* For private mappings, just map in zero pages. */
370 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
373 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
375 if (vma
->vm_flags
& VM_SHARED
)
377 count
= vma
->vm_end
- addr
;
381 zap_page_range(vma
, addr
, count
);
382 zeromap_page_range(vma
, addr
, count
, PAGE_COPY
);
391 up_read(&mm
->mmap_sem
);
393 /* The shared case is hard. Let's do the conventional zeroing. */
395 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
397 return size
+ unwritten
- PAGE_SIZE
;
405 up_read(&mm
->mmap_sem
);
409 static ssize_t
read_zero(struct file
* file
, char * buf
,
410 size_t count
, loff_t
*ppos
)
412 unsigned long left
, unwritten
, written
= 0;
417 if (!access_ok(VERIFY_WRITE
, buf
, count
))
422 /* do we want to be clever? Arbitrary cut-off */
423 if (count
>= PAGE_SIZE
*4) {
424 unsigned long partial
;
426 /* How much left of the page? */
427 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
428 unwritten
= clear_user(buf
, partial
);
429 written
= partial
- unwritten
;
434 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
435 written
+= (left
& PAGE_MASK
) - unwritten
;
438 buf
+= left
& PAGE_MASK
;
441 unwritten
= clear_user(buf
, left
);
442 written
+= left
- unwritten
;
444 return written
? written
: -EFAULT
;
447 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
449 if (vma
->vm_flags
& VM_SHARED
)
450 return shmem_zero_setup(vma
);
451 if (zeromap_page_range(vma
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
455 #else /* CONFIG_MMU */
456 static ssize_t
read_zero(struct file
* file
, char * buf
,
457 size_t count
, loff_t
*ppos
)
465 chunk
= 4096; /* Just for latency reasons */
466 if (clear_user(buf
, chunk
))
475 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
479 #endif /* CONFIG_MMU */
481 static ssize_t
write_full(struct file
* file
, const char * buf
,
482 size_t count
, loff_t
*ppos
)
488 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
489 * can fopen() both devices with "a" now. This was previously impossible.
493 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
495 return file
->f_pos
= 0;
499 * The memory devices use the full 32/64 bits of the offset, and so we cannot
500 * check against negative addresses: they are ok. The return value is weird,
501 * though, in that case (0).
503 * also note that seeking relative to the "end of file" isn't supported:
504 * it has no meaning, so it returns -EINVAL.
506 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
510 down(&file
->f_dentry
->d_inode
->i_sem
);
513 file
->f_pos
= offset
;
515 force_successful_syscall_return();
518 file
->f_pos
+= offset
;
520 force_successful_syscall_return();
525 up(&file
->f_dentry
->d_inode
->i_sem
);
529 static int open_port(struct inode
* inode
, struct file
* filp
)
531 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
534 #define mmap_kmem mmap_mem
535 #define zero_lseek null_lseek
536 #define full_lseek null_lseek
537 #define write_zero write_null
538 #define read_full read_zero
539 #define open_mem open_port
540 #define open_kmem open_mem
542 static struct file_operations mem_fops
= {
543 .llseek
= memory_lseek
,
550 static struct file_operations kmem_fops
= {
551 .llseek
= memory_lseek
,
558 static struct file_operations null_fops
= {
559 .llseek
= null_lseek
,
564 #if defined(CONFIG_ISA) || !defined(__mc68000__)
565 static struct file_operations port_fops
= {
566 .llseek
= memory_lseek
,
573 static struct file_operations zero_fops
= {
574 .llseek
= zero_lseek
,
580 static struct file_operations full_fops
= {
581 .llseek
= full_lseek
,
586 static ssize_t
kmsg_write(struct file
* file
, const char * buf
,
587 size_t count
, loff_t
*ppos
)
592 tmp
= kmalloc(count
+ 1, GFP_KERNEL
);
596 if (!copy_from_user(tmp
, buf
, count
)) {
598 ret
= printk("%s", tmp
);
604 static struct file_operations kmsg_fops
= {
608 static int memory_open(struct inode
* inode
, struct file
* filp
)
610 switch (minor(inode
->i_rdev
)) {
612 filp
->f_op
= &mem_fops
;
615 filp
->f_op
= &kmem_fops
;
618 filp
->f_op
= &null_fops
;
620 #if defined(CONFIG_ISA) || !defined(__mc68000__)
622 filp
->f_op
= &port_fops
;
626 filp
->f_op
= &zero_fops
;
629 filp
->f_op
= &full_fops
;
632 filp
->f_op
= &random_fops
;
635 filp
->f_op
= &urandom_fops
;
638 filp
->f_op
= &kmsg_fops
;
643 if (filp
->f_op
&& filp
->f_op
->open
)
644 return filp
->f_op
->open(inode
,filp
);
648 static struct file_operations memory_fops
= {
649 .open
= memory_open
, /* just a selector for the real open */
652 static const struct {
656 struct file_operations
*fops
;
657 } devlist
[] = { /* list of minor devices */
658 {1, "mem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &mem_fops
},
659 {2, "kmem", S_IRUSR
| S_IWUSR
| S_IRGRP
, &kmem_fops
},
660 {3, "null", S_IRUGO
| S_IWUGO
, &null_fops
},
661 #if defined(CONFIG_ISA) || !defined(__mc68000__)
662 {4, "port", S_IRUSR
| S_IWUSR
| S_IRGRP
, &port_fops
},
664 {5, "zero", S_IRUGO
| S_IWUGO
, &zero_fops
},
665 {7, "full", S_IRUGO
| S_IWUGO
, &full_fops
},
666 {8, "random", S_IRUGO
| S_IWUSR
, &random_fops
},
667 {9, "urandom", S_IRUGO
| S_IWUSR
, &urandom_fops
},
668 {11,"kmsg", S_IRUGO
| S_IWUSR
, &kmsg_fops
},
671 static int __init
chr_dev_init(void)
675 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
676 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
678 for (i
= 0; i
< ARRAY_SIZE(devlist
); i
++) {
679 devfs_mk_cdev(MKDEV(MEM_MAJOR
, devlist
[i
].minor
),
680 S_IFCHR
| devlist
[i
].mode
, devlist
[i
].name
);
684 #if defined (CONFIG_FB)
688 #ifdef CONFIG_M68K_PRINTER
698 fs_initcall(chr_dev_init
);