Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / arch / um / os-Linux / skas / mem.c
1 /*
2  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <sys/mman.h>
11 #include "init.h"
12 #include "kern_constants.h"
13 #include "as-layout.h"
14 #include "mm_id.h"
15 #include "os.h"
16 #include "proc_mm.h"
17 #include "ptrace_user.h"
18 #include "registers.h"
19 #include "skas.h"
20 #include "user.h"
21 #include "sysdep/ptrace.h"
22 #include "sysdep/stub.h"
23 #include "uml-config.h"
24
25 extern unsigned long batch_syscall_stub, __syscall_stub_start;
26
27 extern void wait_stub_done(int pid);
28
29 static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
30                                               unsigned long *stack)
31 {
32         if (stack == NULL) {
33                 stack = (unsigned long *) mm_idp->stack + 2;
34                 *stack = 0;
35         }
36         return stack;
37 }
38
39 static unsigned long syscall_regs[MAX_REG_NR];
40
41 static int __init init_syscall_regs(void)
42 {
43         get_safe_registers(syscall_regs);
44         syscall_regs[REGS_IP_INDEX] = STUB_CODE +
45                 ((unsigned long) &batch_syscall_stub -
46                  (unsigned long) &__syscall_stub_start);
47         return 0;
48 }
49
50 __initcall(init_syscall_regs);
51
52 extern int proc_mm;
53
54 int single_count = 0;
55 int multi_count = 0;
56 int multi_op_count = 0;
57
58 static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr)
59 {
60         int n, i;
61         long ret, offset;
62         unsigned long * data;
63         unsigned long * syscall;
64         int err, pid = mm_idp->u.pid;
65
66         if (proc_mm)
67                 /* FIXME: Need to look up userspace_pid by cpu */
68                 pid = userspace_pid[0];
69
70         multi_count++;
71
72         n = ptrace_setregs(pid, syscall_regs);
73         if (n < 0) {
74                 printk(UM_KERN_ERR "Registers - \n");
75                 for (i = 0; i < MAX_REG_NR; i++)
76                         printk(UM_KERN_ERR "\t%d\t0x%lx\n", i, syscall_regs[i]);
77                 panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
78                       -n);
79         }
80
81         err = ptrace(PTRACE_CONT, pid, 0, 0);
82         if (err)
83                 panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
84                       errno);
85
86         wait_stub_done(pid);
87
88         /*
89          * When the stub stops, we find the following values on the
90          * beginning of the stack:
91          * (long )return_value
92          * (long )offset to failed sycall-data (0, if no error)
93          */
94         ret = *((unsigned long *) mm_idp->stack);
95         offset = *((unsigned long *) mm_idp->stack + 1);
96         if (offset) {
97                 data = (unsigned long *)(mm_idp->stack + offset - STUB_DATA);
98                 printk(UM_KERN_ERR "do_syscall_stub : ret = %ld, offset = %ld, "
99                        "data = %p\n", ret, offset, data);
100                 syscall = (unsigned long *)((unsigned long)data + data[0]);
101                 printk(UM_KERN_ERR "do_syscall_stub: syscall %ld failed, "
102                        "return value = 0x%lx, expected return value = 0x%lx\n",
103                        syscall[0], ret, syscall[7]);
104                 printk(UM_KERN_ERR "    syscall parameters: "
105                        "0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
106                        syscall[1], syscall[2], syscall[3],
107                        syscall[4], syscall[5], syscall[6]);
108                 for (n = 1; n < data[0]/sizeof(long); n++) {
109                         if (n == 1)
110                                 printk(UM_KERN_ERR "    additional syscall "
111                                        "data:");
112                         if (n % 4 == 1)
113                                 printk("\n" UM_KERN_ERR "      ");
114                         printk("  0x%lx", data[n]);
115                 }
116                 if (n > 1)
117                         printk("\n");
118         }
119         else ret = 0;
120
121         *addr = check_init_stack(mm_idp, NULL);
122
123         return ret;
124 }
125
126 long run_syscall_stub(struct mm_id * mm_idp, int syscall,
127                       unsigned long *args, long expected, void **addr,
128                       int done)
129 {
130         unsigned long *stack = check_init_stack(mm_idp, *addr);
131
132         if (done && *addr == NULL)
133                 single_count++;
134
135         *stack += sizeof(long);
136         stack += *stack / sizeof(long);
137
138         *stack++ = syscall;
139         *stack++ = args[0];
140         *stack++ = args[1];
141         *stack++ = args[2];
142         *stack++ = args[3];
143         *stack++ = args[4];
144         *stack++ = args[5];
145         *stack++ = expected;
146         *stack = 0;
147         multi_op_count++;
148
149         if (!done && ((((unsigned long) stack) & ~UM_KERN_PAGE_MASK) <
150                      UM_KERN_PAGE_SIZE - 10 * sizeof(long))) {
151                 *addr = stack;
152                 return 0;
153         }
154
155         return do_syscall_stub(mm_idp, addr);
156 }
157
158 long syscall_stub_data(struct mm_id * mm_idp,
159                        unsigned long *data, int data_count,
160                        void **addr, void **stub_addr)
161 {
162         unsigned long *stack;
163         int ret = 0;
164
165         /*
166          * If *addr still is uninitialized, it *must* contain NULL.
167          * Thus in this case do_syscall_stub correctly won't be called.
168          */
169         if ((((unsigned long) *addr) & ~UM_KERN_PAGE_MASK) >=
170            UM_KERN_PAGE_SIZE - (10 + data_count) * sizeof(long)) {
171                 ret = do_syscall_stub(mm_idp, addr);
172                 /* in case of error, don't overwrite data on stack */
173                 if (ret)
174                         return ret;
175         }
176
177         stack = check_init_stack(mm_idp, *addr);
178         *addr = stack;
179
180         *stack = data_count * sizeof(long);
181
182         memcpy(stack + 1, data, data_count * sizeof(long));
183
184         *stub_addr = (void *)(((unsigned long)(stack + 1) &
185                                ~UM_KERN_PAGE_MASK) + STUB_DATA);
186
187         return 0;
188 }
189
190 int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len, int prot,
191         int phys_fd, unsigned long long offset, int done, void **data)
192 {
193         int ret;
194
195         if (proc_mm) {
196                 struct proc_mm_op map;
197                 int fd = mm_idp->u.mm_fd;
198
199                 map = ((struct proc_mm_op) { .op        = MM_MMAP,
200                                        .u               =
201                                        { .mmap  =
202                                          { .addr        = virt,
203                                            .len = len,
204                                            .prot        = prot,
205                                            .flags       = MAP_SHARED |
206                                            MAP_FIXED,
207                                            .fd  = phys_fd,
208                                            .offset= offset
209                                          } } } );
210                 CATCH_EINTR(ret = write(fd, &map, sizeof(map)));
211                 if (ret != sizeof(map)) {
212                         ret = -errno;
213                         printk(UM_KERN_ERR "map : /proc/mm map failed, "
214                                "err = %d\n", -ret);
215                 }
216                 else ret = 0;
217         }
218         else {
219                 unsigned long args[] = { virt, len, prot,
220                                          MAP_SHARED | MAP_FIXED, phys_fd,
221                                          MMAP_OFFSET(offset) };
222
223                 ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
224                                        data, done);
225         }
226
227         return ret;
228 }
229
230 int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
231           int done, void **data)
232 {
233         int ret;
234
235         if (proc_mm) {
236                 struct proc_mm_op unmap;
237                 int fd = mm_idp->u.mm_fd;
238
239                 unmap = ((struct proc_mm_op) { .op      = MM_MUNMAP,
240                                          .u     =
241                                          { .munmap      =
242                                            { .addr      =
243                                              (unsigned long) addr,
244                                              .len               = len } } } );
245                 CATCH_EINTR(ret = write(fd, &unmap, sizeof(unmap)));
246                 if (ret != sizeof(unmap)) {
247                         ret = -errno;
248                         printk(UM_KERN_ERR "unmap - proc_mm write returned "
249                                "%d\n", ret);
250                 }
251                 else ret = 0;
252         }
253         else {
254                 unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
255                                          0 };
256
257                 ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
258                                        data, done);
259         }
260
261         return ret;
262 }
263
264 int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
265             unsigned int prot, int done, void **data)
266 {
267         struct proc_mm_op protect;
268         int ret;
269
270         if (proc_mm) {
271                 int fd = mm_idp->u.mm_fd;
272
273                 protect = ((struct proc_mm_op) { .op    = MM_MPROTECT,
274                                            .u   =
275                                            { .mprotect  =
276                                              { .addr    =
277                                                (unsigned long) addr,
278                                                .len     = len,
279                                                .prot    = prot } } } );
280
281                 CATCH_EINTR(ret = write(fd, &protect, sizeof(protect)));
282                 if (ret != sizeof(protect)) {
283                         ret = -errno;
284                         printk(UM_KERN_ERR "protect failed, err = %d", -ret);
285                 }
286                 else ret = 0;
287         }
288         else {
289                 unsigned long args[] = { addr, len, prot, 0, 0, 0 };
290
291                 ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0,
292                                        data, done);
293         }
294
295         return ret;
296 }