2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <asm/unistd.h>
20 #include "ptrace_user.h"
21 #include "kern_util.h"
23 #include "registers.h"
24 #include "uml-config.h"
25 #include "sysdep/ptrace.h"
26 #include "sysdep/stub.h"
28 #include "kern_constants.h"
30 extern unsigned long batch_syscall_stub, __syscall_stub_start;
32 extern void wait_stub_done(int pid);
34 static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
38 stack = (unsigned long *) mm_idp->stack + 2;
44 static unsigned long syscall_regs[MAX_REG_NR];
46 static int __init init_syscall_regs(void)
48 get_safe_registers(syscall_regs, NULL);
49 syscall_regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
50 ((unsigned long) &batch_syscall_stub -
51 (unsigned long) &__syscall_stub_start);
55 __initcall(init_syscall_regs);
61 int multi_op_count = 0;
63 static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr)
68 unsigned long * syscall;
69 int err, pid = mm_idp->u.pid;
72 /* FIXME: Need to look up userspace_pid by cpu */
73 pid = userspace_pid[0];
77 n = ptrace_setregs(pid, syscall_regs);
79 printk("Registers - \n");
80 for(i = 0; i < MAX_REG_NR; i++)
81 printk("\t%d\t0x%lx\n", i, syscall_regs[i]);
82 panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
86 err = ptrace(PTRACE_CONT, pid, 0, 0);
88 panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
93 /* When the stub stops, we find the following values on the
94 * beginning of the stack:
96 * (long )offset to failed sycall-data (0, if no error)
98 ret = *((unsigned long *) mm_idp->stack);
99 offset = *((unsigned long *) mm_idp->stack + 1);
101 data = (unsigned long *)(mm_idp->stack +
102 offset - UML_CONFIG_STUB_DATA);
103 printk("do_syscall_stub : ret = %ld, offset = %ld, "
104 "data = %p\n", ret, offset, data);
105 syscall = (unsigned long *)((unsigned long)data + data[0]);
106 printk("do_syscall_stub: syscall %ld failed, return value = "
107 "0x%lx, expected return value = 0x%lx\n",
108 syscall[0], ret, syscall[7]);
109 printk(" syscall parameters: "
110 "0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
111 syscall[1], syscall[2], syscall[3],
112 syscall[4], syscall[5], syscall[6]);
113 for(n = 1; n < data[0]/sizeof(long); n++) {
115 printk(" additional syscall data:");
118 printk(" 0x%lx", data[n]);
125 *addr = check_init_stack(mm_idp, NULL);
130 long run_syscall_stub(struct mm_id * mm_idp, int syscall,
131 unsigned long *args, long expected, void **addr,
134 unsigned long *stack = check_init_stack(mm_idp, *addr);
136 if(done && *addr == NULL)
139 *stack += sizeof(long);
140 stack += *stack / sizeof(long);
153 if(!done && ((((unsigned long) stack) & ~UM_KERN_PAGE_MASK) <
154 UM_KERN_PAGE_SIZE - 10 * sizeof(long))){
159 return do_syscall_stub(mm_idp, addr);
162 long syscall_stub_data(struct mm_id * mm_idp,
163 unsigned long *data, int data_count,
164 void **addr, void **stub_addr)
166 unsigned long *stack;
169 /* If *addr still is uninitialized, it *must* contain NULL.
170 * Thus in this case do_syscall_stub correctly won't be called.
172 if((((unsigned long) *addr) & ~UM_KERN_PAGE_MASK) >=
173 UM_KERN_PAGE_SIZE - (10 + data_count) * sizeof(long)) {
174 ret = do_syscall_stub(mm_idp, addr);
175 /* in case of error, don't overwrite data on stack */
180 stack = check_init_stack(mm_idp, *addr);
183 *stack = data_count * sizeof(long);
185 memcpy(stack + 1, data, data_count * sizeof(long));
187 *stub_addr = (void *)(((unsigned long)(stack + 1) &
188 ~UM_KERN_PAGE_MASK) + UML_CONFIG_STUB_DATA);
193 int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len, int prot,
194 int phys_fd, unsigned long long offset, int done, void **data)
199 struct proc_mm_op map;
200 int fd = mm_idp->u.mm_fd;
202 map = ((struct proc_mm_op) { .op = MM_MMAP,
208 .flags = MAP_SHARED |
213 CATCH_EINTR(ret = write(fd, &map, sizeof(map)));
214 if(ret != sizeof(map)){
216 printk("map : /proc/mm map failed, err = %d\n", -ret);
221 unsigned long args[] = { virt, len, prot,
222 MAP_SHARED | MAP_FIXED, phys_fd,
223 MMAP_OFFSET(offset) };
225 ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
232 int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
233 int done, void **data)
238 struct proc_mm_op unmap;
239 int fd = mm_idp->u.mm_fd;
241 unmap = ((struct proc_mm_op) { .op = MM_MUNMAP,
245 (unsigned long) addr,
247 CATCH_EINTR(ret = write(fd, &unmap, sizeof(unmap)));
248 if(ret != sizeof(unmap)){
250 printk("unmap - proc_mm write returned %d\n", ret);
255 unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
258 ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
265 int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
266 unsigned int prot, int done, void **data)
268 struct proc_mm_op protect;
272 int fd = mm_idp->u.mm_fd;
274 protect = ((struct proc_mm_op) { .op = MM_MPROTECT,
278 (unsigned long) addr,
280 .prot = prot } } } );
282 CATCH_EINTR(ret = write(fd, &protect, sizeof(protect)));
283 if(ret != sizeof(protect)){
285 printk("protect failed, err = %d", -ret);
290 unsigned long args[] = { addr, len, prot, 0, 0, 0 };
292 ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0,
299 void before_mem_skas(unsigned long unused)