13 #include <linux/slab.h>
15 #include <linux/mman.h>
16 #include <linux/random.h>
19 #include <linux/tty.h>
20 #include <linux/capability.h>
21 #include <linux/ptrace.h>
22 #include <linux/device.h>
29 #include <linux/export.h>
32 #include <asm/uaccess.h>
38 #define DEVPORT_MINOR 4
40 static inline unsigned long size_inside_page(
unsigned long start,
50 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
53 return addr + count <=
__pa(high_memory);
62 #ifdef CONFIG_STRICT_DEVMEM
63 static inline int range_is_allowed(
unsigned long pfn,
unsigned long size)
72 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
82 static inline int range_is_allowed(
unsigned long pfn,
unsigned long size)
97 size_t count, loff_t *ppos)
99 unsigned long p = *ppos;
106 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
109 sz = size_inside_page(p, count);
122 unsigned long remaining;
124 sz = size_inside_page(p, count);
126 if (!range_is_allowed(p >>
PAGE_SHIFT, count))
153 static ssize_t write_mem(
struct file *file,
const char __user *buf,
154 size_t count, loff_t *ppos)
156 unsigned long p = *ppos;
158 unsigned long copied;
166 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
169 sz = size_inside_page(p, count);
179 sz = size_inside_page(p, count);
199 written += sz - copied;
216 unsigned long pfn,
unsigned long size,
pgprot_t *vma_prot)
221 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
228 #ifdef pgprot_noncached
229 static int uncached_access(
struct file *file,
unsigned long addr)
231 #if defined(CONFIG_IA64)
237 #elif defined(CONFIG_MIPS)
252 return addr >=
__pa(high_memory);
258 unsigned long size,
pgprot_t vma_prot)
260 #ifdef pgprot_noncached
263 if (uncached_access(file, offset))
271 static unsigned long get_unmapped_area_mem(
struct file *file,
278 return (
unsigned long) -
EINVAL;
288 #define get_unmapped_area_mem NULL
296 static const struct vm_operations_struct mmap_mem_ops = {
297 #ifdef CONFIG_HAVE_IOREMAP_PROT
298 .access = generic_access_phys
302 static int mmap_mem(
struct file *file,
struct vm_area_struct *vma)
309 if (!private_mapping_ok(vma))
312 if (!range_is_allowed(vma->
vm_pgoff, size))
323 vma->
vm_ops = &mmap_mem_ops;
336 #ifdef CONFIG_DEVKMEM
337 static int mmap_kmem(
struct file *file,
struct vm_area_struct *vma)
355 return mmap_mem(file, vma);
359 #ifdef CONFIG_CRASH_DUMP
363 static ssize_t read_oldmem(
struct file *file,
char __user *buf,
364 size_t count, loff_t *ppos)
366 unsigned long pfn,
offset;
367 size_t read = 0, csize;
376 if (count > PAGE_SIZE - offset)
377 csize = PAGE_SIZE -
offset;
393 #ifdef CONFIG_DEVKMEM
397 static ssize_t read_kmem(
struct file *file,
char __user *buf,
398 size_t count, loff_t *ppos)
400 unsigned long p = *ppos;
406 if (p < (
unsigned long) high_memory) {
408 if (count > (
unsigned long)high_memory - p)
409 low_count = (
unsigned long)high_memory - p;
411 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
413 if (p < PAGE_SIZE && low_count > 0) {
414 sz = size_inside_page(p, low_count);
424 while (low_count > 0) {
425 sz = size_inside_page(p, low_count);
449 sz = size_inside_page(p, count);
454 sz =
vread(kbuf, (
char *)p, sz);
469 return read ? read :
err;
473 static ssize_t do_write_kmem(
unsigned long p,
const char __user *buf,
474 size_t count, loff_t *ppos)
477 unsigned long copied;
480 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
483 sz = size_inside_page(p, count);
495 sz = size_inside_page(p, count);
506 written += sz - copied;
524 static ssize_t write_kmem(
struct file *file,
const char __user *buf,
525 size_t count, loff_t *ppos)
527 unsigned long p = *ppos;
533 if (p < (
unsigned long) high_memory) {
534 unsigned long to_write =
min_t(
unsigned long, count,
535 (
unsigned long)high_memory - p);
536 wrote = do_write_kmem(p, buf, to_write, ppos);
537 if (wrote != to_write)
547 return wrote ? wrote : -
ENOMEM;
549 unsigned long sz = size_inside_page(p, count);
561 vwrite(kbuf, (
char *)p, sz);
571 return virtr + wrote ? :
err;
575 #ifdef CONFIG_DEVPORT
576 static ssize_t read_port(
struct file *file,
char __user *buf,
577 size_t count, loff_t *ppos)
579 unsigned long i = *ppos;
584 while (count-- > 0 && i < 65536) {
594 static ssize_t write_port(
struct file *file,
const char __user *buf,
595 size_t count, loff_t *ppos)
597 unsigned long i = *ppos;
602 while (count-- > 0 && i < 65536) {
618 static ssize_t read_null(
struct file *file,
char __user *buf,
619 size_t count, loff_t *ppos)
624 static ssize_t write_null(
struct file *file,
const char __user *buf,
625 size_t count, loff_t *ppos)
637 loff_t *ppos,
size_t len,
unsigned int flags)
642 static ssize_t read_zero(
struct file *file,
char __user *buf,
643 size_t count, loff_t *ppos)
655 unsigned long unwritten;
658 if (chunk > PAGE_SIZE)
661 written += chunk - unwritten;
670 return written ? written : -
EFAULT;
673 static int mmap_zero(
struct file *file,
struct vm_area_struct *vma)
683 static ssize_t write_full(
struct file *file,
const char __user *buf,
684 size_t count, loff_t *ppos)
694 static loff_t null_lseek(
struct file *file, loff_t offset,
int orig)
696 return file->
f_pos = 0;
707 static loff_t memory_lseek(
struct file *file, loff_t offset,
int orig)
714 offset += file->
f_pos;
717 if ((
unsigned long long)offset >= ~0xFFFULL) {
732 static int open_port(
struct inode *
inode,
struct file * filp)
737 #define zero_lseek null_lseek
738 #define full_lseek null_lseek
739 #define write_zero write_null
740 #define read_full read_zero
741 #define open_mem open_port
742 #define open_kmem open_mem
743 #define open_oldmem open_mem
746 .llseek = memory_lseek,
751 .get_unmapped_area = get_unmapped_area_mem,
754 #ifdef CONFIG_DEVKMEM
761 .get_unmapped_area = get_unmapped_area_mem,
766 .llseek = null_lseek,
769 .splice_write = splice_write_null,
772 #ifdef CONFIG_DEVPORT
804 #ifdef CONFIG_CRASH_DUMP
812 static const struct memdev {
819 #ifdef CONFIG_DEVKMEM
822 [3] = {
"null", 0666, &null_fops,
NULL },
823 #ifdef CONFIG_DEVPORT
824 [4] = {
"port", 0, &port_fops,
NULL },
826 [5] = {
"zero", 0666, &zero_fops, &zero_bdi },
827 [7] = {
"full", 0666, &full_fops,
NULL },
833 #ifdef CONFIG_CRASH_DUMP
834 [12] = {
"oldmem", 0, &oldmem_fops,
NULL },
838 static int memory_open(
struct inode *
inode,
struct file *filp)
843 minor = iminor(inode);
851 filp->
f_op = dev->fops;
853 filp->
f_mapping->backing_dev_info = dev->dev_info;
860 return dev->fops->open(inode, filp);
877 static struct class *mem_class;
879 static int __init chr_dev_init(
void)
888 if (register_chrdev(
MEM_MAJOR,
"mem", &memory_fops))
892 if (IS_ERR(mem_class))
893 return PTR_ERR(mem_class);
895 mem_class->
devnode = mem_devnode;