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