Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / xen / char / mem.c
1 /*
2  *  Originally from 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/mm.h>
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>
26 #include <asm/io.h>
27 #include <asm/hypervisor.h>
28
29 static inline int uncached_access(struct file *file)
30 {
31         if (file->f_flags & O_SYNC)
32                 return 1;
33         /* Xen sets correct MTRR type on non-RAM for us. */
34         return 0;
35 }
36
37 /*
38  * This funcion reads the *physical* memory. The f_pos points directly to the 
39  * memory location. 
40  */
41 static ssize_t read_mem(struct file * file, char __user * buf,
42                         size_t count, loff_t *ppos)
43 {
44         unsigned long p = *ppos, ignored;
45         ssize_t read = 0, sz;
46         void __iomem *v;
47
48         while (count > 0) {
49                 /*
50                  * Handle first page in case it's not aligned
51                  */
52                 if (-p & (PAGE_SIZE - 1))
53                         sz = -p & (PAGE_SIZE - 1);
54                 else
55                         sz = PAGE_SIZE;
56
57                 sz = min_t(unsigned long, sz, count);
58
59                 if ((v = ioremap(p, sz)) == NULL) {
60                         /*
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. :-)
66                          */
67                         if (clear_user(buf, count))
68                                 return -EFAULT;
69                         read += count;
70                         break;
71                 }
72
73                 ignored = copy_to_user(buf, v, sz);
74                 iounmap(v);
75                 if (ignored)
76                         return -EFAULT;
77                 buf += sz;
78                 p += sz;
79                 count -= sz;
80                 read += sz;
81         }
82
83         *ppos += read;
84         return read;
85 }
86
87 static ssize_t write_mem(struct file * file, const char __user * buf, 
88                          size_t count, loff_t *ppos)
89 {
90         unsigned long p = *ppos, ignored;
91         ssize_t written = 0, sz;
92         void __iomem *v;
93
94         while (count > 0) {
95                 /*
96                  * Handle first page in case it's not aligned
97                  */
98                 if (-p & (PAGE_SIZE - 1))
99                         sz = -p & (PAGE_SIZE - 1);
100                 else
101                         sz = PAGE_SIZE;
102
103                 sz = min_t(unsigned long, sz, count);
104
105                 if ((v = ioremap(p, sz)) == NULL)
106                         break;
107
108                 ignored = copy_from_user(v, buf, sz);
109                 iounmap(v);
110                 if (ignored) {
111                         written += sz - ignored;
112                         if (written)
113                                 break;
114                         return -EFAULT;
115                 }
116                 buf += sz;
117                 p += sz;
118                 count -= sz;
119                 written += sz;
120         }
121
122         *ppos += written;
123         return written;
124 }
125
126 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
127 {
128         size_t size = vma->vm_end - vma->vm_start;
129
130         if (uncached_access(file))
131                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
132
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);
136 }
137
138 /*
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).
142  *
143  * also note that seeking relative to the "end of file" isn't supported:
144  * it has no meaning, so it returns -EINVAL.
145  */
146 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
147 {
148         loff_t ret;
149
150         mutex_lock(&file->f_dentry->d_inode->i_mutex);
151         switch (orig) {
152                 case 0:
153                         file->f_pos = offset;
154                         ret = file->f_pos;
155                         force_successful_syscall_return();
156                         break;
157                 case 1:
158                         file->f_pos += offset;
159                         ret = file->f_pos;
160                         force_successful_syscall_return();
161                         break;
162                 default:
163                         ret = -EINVAL;
164         }
165         mutex_unlock(&file->f_dentry->d_inode->i_mutex);
166         return ret;
167 }
168
169 static int open_mem(struct inode * inode, struct file * filp)
170 {
171         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
172 }
173
174 struct file_operations mem_fops = {
175         .llseek         = memory_lseek,
176         .read           = read_mem,
177         .write          = write_mem,
178         .mmap           = mmap_mem,
179         .open           = open_mem,
180 };