2 * Originally from 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>
12 #include <linux/miscdevice.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mman.h>
16 #include <linux/random.h>
17 #include <linux/init.h>
18 #include <linux/raw.h>
19 #include <linux/tty.h>
20 #include <linux/capability.h>
21 #include <linux/smp_lock.h>
22 #include <linux/ptrace.h>
23 #include <linux/device.h>
24 #include <asm/pgalloc.h>
25 #include <asm/uaccess.h>
27 #include <asm/hypervisor.h>
29 static inline int uncached_access(struct file *file)
31 if (file->f_flags & O_SYNC)
33 /* Xen sets correct MTRR type on non-RAM for us. */
38 * This funcion reads the *physical* memory. The f_pos points directly to the
41 static ssize_t read_mem(struct file * file, char __user * buf,
42 size_t count, loff_t *ppos)
44 unsigned long p = *ppos, ignored;
50 * Handle first page in case it's not aligned
52 if (-p & (PAGE_SIZE - 1))
53 sz = -p & (PAGE_SIZE - 1);
57 sz = min_t(unsigned long, sz, count);
59 if ((v = ioremap(p, sz)) == NULL) {
61 * Some programs (e.g., dmidecode) groove off into weird RAM
62 * areas where no tables can possibly exist (because Xen will
63 * have stomped on them!). These programs get rather upset if
64 * we let them know that Xen failed their access, so we fake
65 * out a read of all zeroes. :-)
67 if (clear_user(buf, count))
73 ignored = copy_to_user(buf, v, sz);
87 static ssize_t write_mem(struct file * file, const char __user * buf,
88 size_t count, loff_t *ppos)
90 unsigned long p = *ppos, ignored;
91 ssize_t written = 0, sz;
96 * Handle first page in case it's not aligned
98 if (-p & (PAGE_SIZE - 1))
99 sz = -p & (PAGE_SIZE - 1);
103 sz = min_t(unsigned long, sz, count);
105 if ((v = ioremap(p, sz)) == NULL)
108 ignored = copy_from_user(v, buf, sz);
111 written += sz - ignored;
126 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
128 size_t size = vma->vm_end - vma->vm_start;
130 if (uncached_access(file))
131 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
133 /* We want to return the real error code, not EAGAIN. */
134 return direct_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
135 size, vma->vm_page_prot, DOMID_IO);
139 * The memory devices use the full 32/64 bits of the offset, and so we cannot
140 * check against negative addresses: they are ok. The return value is weird,
141 * though, in that case (0).
143 * also note that seeking relative to the "end of file" isn't supported:
144 * it has no meaning, so it returns -EINVAL.
146 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
150 mutex_lock(&file->f_dentry->d_inode->i_mutex);
153 file->f_pos = offset;
155 force_successful_syscall_return();
158 file->f_pos += offset;
160 force_successful_syscall_return();
165 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
169 static int open_mem(struct inode * inode, struct file * filp)
171 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
174 struct file_operations mem_fops = {
175 .llseek = memory_lseek,