1 /* $NetBSD: linux_ptrace.c,v 1.23 2010/07/01 02:38:28 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matthias Scheler and Emmanuel Dreyfus. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.23 2010/07/01 02:38:28 rmind Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/malloc.h> 37 #include <sys/mount.h> 38 #include <sys/proc.h> 39 #include <sys/ptrace.h> 40 #include <sys/systm.h> 41 #include <sys/syscallargs.h> 42 #include <uvm/uvm_extern.h> 43 44 #include <machine/reg.h> 45 46 #include <compat/linux/common/linux_types.h> 47 #include <compat/linux/common/linux_ptrace.h> 48 #include <compat/linux/common/linux_signal.h> 49 50 #include <compat/linux/common/linux_util.h> 51 #include <compat/linux/common/linux_machdep.h> 52 #include <compat/linux/common/linux_emuldata.h> 53 #include <compat/linux/common/linux_exec.h> /* for emul_linux */ 54 55 #include <compat/linux/linux_syscallargs.h> 56 57 #include <lib/libkern/libkern.h> /* for offsetof() */ 58 59 /* 60 * From Linux's include/asm-ppc/ptrace.h. 61 * structure used for storing reg context: defined in linux_machdep.h 62 * structure used for storing floating point context: 63 * Linux just uses a double fpr[32], no struct 64 */ 65 66 /* 67 * user struct for linux process - this is used for Linux ptrace emulation 68 * most of it is junk only used by gdb 69 * 70 * From Linux's include/asm-ppc/user.h 71 * 72 * XXX u_ar0 was a struct reg in Linux/powerpc 73 * Can't find out what a struct regs is for Linux/powerpc, 74 * so we use a struct pt_regs instead. don't know if this is right. 75 */ 76 77 struct linux_user { 78 struct linux_pt_regs regs; 79 #define lusr_startgdb regs 80 size_t u_tsize; 81 size_t u_dsize; 82 size_t u_ssize; 83 unsigned long start_code; 84 unsigned long start_data; 85 unsigned long start_stack; 86 long int signal; 87 struct linux_pt_regs *u_ar0; /* help gdb find registers */ 88 unsigned long magic; 89 char u_comm[32]; 90 #define lu_comm_end u_comm[31] 91 }; 92 93 #define LUSR_OFF(member) offsetof(struct linux_user, member) 94 #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member) 95 #define ISSET(t, f) ((t) & (f)) 96 97 int linux_ptrace_disabled = 1; /* bitrotted */ 98 99 /* XXX Check me! (From NetBSD/i386) */ 100 int 101 linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap, 102 register_t *retval) 103 { 104 /* { 105 syscallarg(int) request; 106 syscallarg(int) pid; 107 syscallarg(int) addr; 108 syscallarg(int) data; 109 } */ 110 struct proc *p = l->l_proc, *t; 111 struct lwp *lt; 112 struct reg *regs = NULL; 113 struct fpreg *fpregs = NULL; 114 struct linux_pt_regs *linux_regs = NULL; 115 double *linux_fpreg = NULL; /* it's an array, not a struct */ 116 int request, error, addr, i; 117 118 if (linux_ptrace_disabled) 119 return ENOSYS; 120 121 error = 0; 122 request = SCARG(uap, request); 123 124 switch (request) { 125 case LINUX_PTRACE_PEEKUSR: 126 case LINUX_PTRACE_POKEUSR: 127 regs = kmem_alloc(sizeof(struct reg), KM_SLEEP); 128 break; 129 case LINUX_PTRACE_GETREGS: 130 case LINUX_PTRACE_SETREGS: 131 regs = kmem_alloc(sizeof(struct reg), KM_SLEEP); 132 linux_regs = kmem_alloc(sizeof(*linux_regs), KM_SLEEP); 133 if (request == LINUX_PTRACE_SETREGS) { 134 error = copyin((void *)SCARG(uap, data), linux_regs, 135 sizeof(struct linux_pt_regs)); 136 if (error) { 137 goto out; 138 } 139 } 140 break; 141 case LINUX_PTRACE_GETFPREGS: 142 case LINUX_PTRACE_SETFPREGS: 143 fpregs = kmem_alloc(sizeof(struct fpreg), KM_SLEEP); 144 linux_fpreg = kmem_alloc(32 * sizeof(double), KM_SLEEP); 145 if (request == LINUX_PTRACE_SETFPREGS) { 146 error = copyin((void *)SCARG(uap, data), linux_fpreg, 147 32 * sizeof(double)); 148 if (error) { 149 goto out; 150 } 151 } 152 break; 153 default: 154 error = EIO; 155 goto out; 156 } 157 158 /* Find the process we are supposed to be operating on. */ 159 mutex_enter(proc_lock); 160 if ((t = proc_find(SCARG(uap, pid))) == NULL) { 161 mutex_exit(proc_lock); 162 return ESRCH; 163 } 164 mutex_enter(t->p_lock); 165 mutex_exit(proc_lock); 166 167 /* 168 * You cannot do what you want to the process if: 169 * 1. It is not being traced at all, 170 */ 171 if (!ISSET(t->p_slflag, PSL_TRACED)) { 172 mutex_exit(t->p_lock); 173 error = EPERM; 174 goto out; 175 } 176 /* 177 * 2. It is being traced by procfs (which has different signal 178 * delivery semantics), 179 * 3. It is not being traced by _you_, or 180 * 4. It is not currently stopped. 181 */ 182 if (ISSET(t->p_slflag, PSL_FSTRACE) || t->p_pptr != p || 183 t->p_stat != SSTOP || !t->p_waited) { 184 mutex_exit(t->p_lock); 185 error = EBUSY; 186 goto out; 187 } 188 /* XXX: ptrace needs revamp for multi-threading support. */ 189 if (t->p_nlwps > 1) { 190 mutex_exit(t->p_lock); 191 error = ENOSYS; 192 goto out; 193 } 194 lt = LIST_FIRST(&t->p_lwps); 195 *retval = 0; 196 197 switch (request) { 198 case LINUX_PTRACE_GETREGS: 199 error = process_read_regs(lt, regs); 200 mutex_exit(t->p_lock); 201 if (error) { 202 break; 203 } 204 for (i = 0; i <= 31; i++) { 205 linux_regs->lgpr[i] = regs->fixreg[i]; 206 } 207 linux_regs->lnip = regs->pc; 208 linux_regs->lmsr = 0; 209 /* XXX Is that right? */ 210 linux_regs->lorig_gpr3 = regs->fixreg[3]; 211 linux_regs->lctr = regs->ctr; 212 linux_regs->llink = regs->lr; 213 linux_regs->lxer = regs->xer; 214 linux_regs->lccr = regs->cr; 215 linux_regs->lmq = 0; 216 linux_regs->ltrap = 0; 217 linux_regs->ldar = 0; 218 linux_regs->ldsisr = 0; 219 linux_regs->lresult = 0; 220 221 error = copyout(linux_regs, (void *)SCARG(uap, data), 222 sizeof(struct linux_pt_regs)); 223 break; 224 225 case LINUX_PTRACE_SETREGS: 226 for (i = 0; i <= 31; i++) { 227 regs->fixreg[i] = linux_regs->lgpr[i]; 228 } 229 regs->lr = linux_regs->llink; 230 regs->cr = linux_regs->lccr; 231 regs->xer = linux_regs->lxer; 232 regs->ctr = linux_regs->lctr; 233 regs->pc = linux_regs->lnip; /* XXX */ 234 235 error = process_write_regs(lt, regs); 236 mutex_exit(t->p_lock); 237 break; 238 239 case LINUX_PTRACE_GETFPREGS: 240 error = process_read_fpregs(lt, fpregs); 241 mutex_exit(t->p_lock); 242 if (error) { 243 break; 244 } 245 /* Zero the contents if NetBSD fpreg structure is smaller */ 246 if (sizeof(struct fpreg) < (32 * sizeof(double))) { 247 memset(linux_fpreg, '\0', (32 * sizeof(double))); 248 } 249 memcpy(linux_fpreg, fpregs, 250 min(32 * sizeof(double), sizeof(struct fpreg))); 251 error = copyout(linux_fpreg, (void *)SCARG(uap, data), 252 32 * sizeof(double)); 253 break; 254 255 case LINUX_PTRACE_SETFPREGS: 256 memset(fpregs, '\0', sizeof(struct fpreg)); 257 memcpy(fpregs, linux_fpreg, 258 min(32 * sizeof(double), sizeof(struct fpreg))); 259 error = process_write_fpregs(lt, fpregs); 260 mutex_exit(t->p_lock); 261 break; 262 263 case LINUX_PTRACE_PEEKUSR: 264 addr = SCARG(uap, addr); 265 error = process_read_regs(lt, regs); 266 mutex_exit(t->p_lock); 267 if (error) { 268 break; 269 } 270 error = 0; 271 if ((addr < LUSR_OFF(lusr_startgdb)) || 272 (addr > LUSR_OFF(lu_comm_end))) 273 error = 1; 274 else if (addr == LUSR_OFF(u_tsize)) 275 *retval = p->p_vmspace->vm_tsize; 276 else if (addr == LUSR_OFF(u_dsize)) 277 *retval = p->p_vmspace->vm_dsize; 278 else if (addr == LUSR_OFF(u_ssize)) 279 *retval = p->p_vmspace->vm_ssize; 280 else if (addr == LUSR_OFF(start_code)) 281 *retval = (register_t) p->p_vmspace->vm_taddr; 282 else if (addr == LUSR_OFF(start_stack)) 283 *retval = (register_t) p->p_vmspace->vm_minsaddr; 284 else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) && 285 (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end))) 286 *retval = regs->fixreg[addr / sizeof (register_t)]; 287 else if (addr == LUSR_REG_OFF(lnip)) 288 *retval = regs->pc; 289 else if (addr == LUSR_REG_OFF(lctr)) 290 *retval = regs->ctr; 291 else if (addr == LUSR_REG_OFF(llink)) 292 *retval = regs->lr; 293 else if (addr == LUSR_REG_OFF(lxer)) 294 *retval = regs->xer; 295 else if (addr == LUSR_REG_OFF(lccr)) 296 *retval = regs->cr; 297 else if (addr == LUSR_OFF(signal)) 298 error = 1; 299 else if (addr == LUSR_OFF(signal)) 300 error = 1; 301 else if (addr == LUSR_OFF(magic)) 302 error = 1; 303 else if (addr == LUSR_OFF(u_comm)) 304 error = 1; 305 else { 306 #ifdef DEBUG_LINUX 307 printf("linux_ptrace: unsupported address: %d\n", addr); 308 #endif 309 error = 1; 310 } 311 if (error) { 312 break; 313 } 314 error = copyout (retval, (void *)SCARG(uap, data), 315 sizeof(retval)); 316 *retval = SCARG(uap, data); 317 break; 318 319 case LINUX_PTRACE_POKEUSR: /* XXX Not tested */ 320 addr = SCARG(uap, addr); 321 error = process_read_regs(lt, regs); 322 if (error) { 323 mutex_exit(t->p_lock); 324 break; 325 } 326 error = 0; 327 if ((addr < LUSR_OFF(lusr_startgdb)) || 328 (addr > LUSR_OFF(lu_comm_end))) 329 error = 1; 330 else if (addr == LUSR_OFF(u_tsize)) 331 error = 1; 332 else if (addr == LUSR_OFF(u_dsize)) 333 error = 1; 334 else if (addr == LUSR_OFF(u_ssize)) 335 error = 1; 336 else if (addr == LUSR_OFF(start_code)) 337 error = 1; 338 else if (addr == LUSR_OFF(start_stack)) 339 error = 1; 340 else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) && 341 (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end))) 342 regs->fixreg[addr / sizeof (register_t)] = 343 (register_t)SCARG(uap, data); 344 else if (addr == LUSR_REG_OFF(lnip)) 345 regs->pc = (register_t)SCARG(uap, data); 346 else if (addr == LUSR_REG_OFF(lctr)) 347 regs->ctr = (register_t)SCARG(uap, data); 348 else if (addr == LUSR_REG_OFF(llink)) 349 regs->lr = (register_t)SCARG(uap, data); 350 else if (addr == LUSR_REG_OFF(lxer)) 351 regs->xer = (register_t)SCARG(uap, data); 352 else if (addr == LUSR_REG_OFF(lccr)) 353 regs->cr = (register_t)SCARG(uap, data); 354 else if (addr == LUSR_OFF(signal)) 355 error = 1; 356 else if (addr == LUSR_OFF(magic)) 357 error = 1; 358 else if (addr == LUSR_OFF(u_comm)) 359 error = 1; 360 else { 361 #ifdef DEBUG_LINUX 362 printf("linux_ptrace: unsupported address: %d\n", addr); 363 #endif 364 error = 1; 365 } 366 error = process_write_regs(lt,regs); 367 mutex_exit(t->p_lock); 368 break; 369 default: 370 mutex_exit(t->p_lock); 371 break; 372 } 373 out: 374 if (regs) 375 kmem_free(regs, sizeof(*regs)); 376 if (fpregs) 377 kmem_free(fpregs, sizeof(*fpregs)); 378 if (linux_regs) 379 kmem_free(linux_regs, sizeof(*linux_regs)); 380 if (linux_fpreg) 381 kmem_free(linux_fpreg, sizeof(*linux_fpreg)); 382 return error; 383 } 384