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