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