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