1 /* $NetBSD: linux_ptrace.c,v 1.8 2001/11/13 02:08:46 lukem 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.8 2001/11/13 02:08:46 lukem Exp $"); 41 42 #include <sys/types.h> 43 #include <sys/param.h> 44 #include <sys/malloc.h> 45 #include <sys/mount.h> 46 #include <sys/proc.h> 47 #include <sys/ptrace.h> 48 #include <sys/systm.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(p, v, retval) /* XXX Check me! (From NetBSD/i386) */ 107 struct proc *p; 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 *t; /* target process */ 119 struct reg *regs = NULL; 120 struct fpreg *fpregs = NULL; 121 struct linux_pt_regs *linux_regs = NULL; 122 double *linux_fpreg = NULL; /* it's an array, not a struct */ 123 int addr; 124 int i; 125 126 switch (request = SCARG(uap, request)) { 127 case LINUX_PTRACE_PEEKUSR: 128 case LINUX_PTRACE_POKEUSR: 129 case LINUX_PTRACE_GETREGS: 130 case LINUX_PTRACE_SETREGS: 131 case LINUX_PTRACE_GETFPREGS: 132 case LINUX_PTRACE_SETFPREGS: 133 break; 134 default: 135 return EIO; 136 } 137 138 /* Find the process we're supposed to be operating on. */ 139 if ((t = pfind(SCARG(uap, pid))) == NULL) 140 return ESRCH; 141 142 /* 143 * You can't do what you want to the process if: 144 * (1) It's not being traced at all, 145 */ 146 if (!ISSET(t->p_flag, P_TRACED)) 147 return EPERM; 148 149 /* 150 * (2) it's being traced by procfs (which has 151 * different signal delivery semantics), 152 */ 153 if (ISSET(t->p_flag, P_FSTRACE)) 154 return EBUSY; 155 156 /* 157 * (3) it's not being traced by _you_, or 158 */ 159 if (t->p_pptr != p) 160 return EBUSY; 161 162 /* 163 * (4) it's not currently stopped. 164 */ 165 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED)) 166 return EBUSY; 167 168 *retval = 0; 169 170 switch (request) { 171 case LINUX_PTRACE_GETREGS: 172 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK); 173 MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs), 174 M_TEMP, M_WAITOK); 175 176 error = process_read_regs(t, regs); 177 if (error != 0) 178 goto out; 179 180 for (i=0; i<=31; i++) 181 linux_regs->lgpr[i] = regs->fixreg[i]; 182 linux_regs->lnip = regs->pc; 183 linux_regs->lmsr = 0; 184 /* XXX Is that right? */ 185 linux_regs->lorig_gpr3 = regs->fixreg[3]; 186 linux_regs->lctr = regs->ctr; 187 linux_regs->llink = regs->lr; 188 linux_regs->lxer = regs->xer; 189 linux_regs->lccr = regs->cr; 190 linux_regs->lmq = 0; 191 linux_regs->ltrap = 0; 192 linux_regs->ldar = 0; 193 linux_regs->ldsisr = 0; 194 linux_regs->lresult = 0; 195 196 error = copyout(linux_regs, (caddr_t)SCARG(uap, data), 197 sizeof(struct linux_pt_regs)); 198 goto out; 199 200 case LINUX_PTRACE_SETREGS: 201 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK); 202 MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs), 203 M_TEMP, M_WAITOK); 204 205 error = copyin((caddr_t)SCARG(uap, data), linux_regs, 206 sizeof(struct linux_pt_regs)); 207 if (error != 0) 208 goto out; 209 210 for (i=0; i<=31; i++) 211 regs->fixreg[i] = linux_regs->lgpr[i]; 212 regs->lr = linux_regs->llink; 213 regs->cr = linux_regs->lccr; 214 regs->xer = linux_regs->lxer; 215 regs->ctr = linux_regs->lctr; 216 regs->pc = linux_regs->lnip; /* XXX */ 217 218 error = process_write_regs(t, regs); 219 goto out; 220 221 case LINUX_PTRACE_GETFPREGS: 222 MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg), 223 M_TEMP, M_WAITOK); 224 MALLOC(linux_fpreg, double *, 225 32*sizeof(double), M_TEMP, M_WAITOK); 226 227 error = process_read_fpregs(t, fpregs); 228 if (error != 0) 229 goto out; 230 231 /* zero the contents if NetBSD fpreg structure is smaller */ 232 if (sizeof(struct fpreg) < (32*sizeof(double))) 233 memset(linux_fpreg, '\0', (32*sizeof(double))); 234 235 memcpy(linux_fpreg, fpregs, 236 min(32*sizeof(double), sizeof(struct fpreg))); 237 error = copyout(linux_fpreg, (caddr_t)SCARG(uap, data), 238 32*sizeof(double)); 239 goto out; 240 241 case LINUX_PTRACE_SETFPREGS: 242 MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg), 243 M_TEMP, M_WAITOK); 244 MALLOC(linux_fpreg, double *, 245 32*sizeof(double), M_TEMP, M_WAITOK); 246 error = copyin((caddr_t)SCARG(uap, data), linux_fpreg, 247 32*sizeof(double)); 248 if (error != 0) 249 goto out; 250 251 memset(fpregs, '\0', sizeof(struct fpreg)); 252 memcpy(fpregs, linux_fpreg, 253 min(32*sizeof(double), sizeof(struct fpreg))); 254 255 error = process_write_fpregs(t, fpregs); 256 goto out; 257 258 case LINUX_PTRACE_PEEKUSR: 259 addr = SCARG(uap, addr); 260 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK); 261 error = process_read_regs(t, regs); 262 if (error) 263 goto out; 264 265 PHOLD(t); /* need full process info */ 266 error = 0; 267 if ((addr < LUSR_OFF(lusr_startgdb)) || 268 (addr > LUSR_OFF(lu_comm_end))) 269 error = 1; 270 else if (addr == LUSR_OFF(u_tsize)) 271 *retval = p->p_vmspace->vm_tsize; 272 else if (addr == LUSR_OFF(u_dsize)) 273 *retval = p->p_vmspace->vm_dsize; 274 else if (addr == LUSR_OFF(u_ssize)) 275 *retval = p->p_vmspace->vm_ssize; 276 else if (addr == LUSR_OFF(start_code)) 277 *retval = (register_t) p->p_vmspace->vm_taddr; 278 else if (addr == LUSR_OFF(start_stack)) 279 *retval = (register_t) p->p_vmspace->vm_minsaddr; 280 else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) && 281 (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end))) 282 *retval = regs->fixreg[addr / sizeof (register_t)]; 283 else if (addr == LUSR_REG_OFF(lnip)) 284 *retval = regs->pc; 285 else if (addr == LUSR_REG_OFF(lctr)) 286 *retval = regs->ctr; 287 else if (addr == LUSR_REG_OFF(llink)) 288 *retval = regs->lr; 289 else if (addr == LUSR_REG_OFF(lxer)) 290 *retval = regs->xer; 291 else if (addr == LUSR_REG_OFF(lccr)) 292 *retval = regs->cr; 293 else if (addr == LUSR_OFF(signal)) 294 error = 1; 295 else if (addr == LUSR_OFF(signal)) 296 error = 1; 297 else if (addr == LUSR_OFF(magic)) 298 error = 1; 299 else if (addr == LUSR_OFF(u_comm)) 300 error = 1; 301 else { 302 #ifdef DEBUG_LINUX 303 printf("linux_ptrace: unsupported address: %d\n", addr); 304 #endif 305 error = 1; 306 } 307 308 PRELE(t); 309 310 if (error) 311 goto out; 312 313 error = copyout (retval, 314 (caddr_t)SCARG(uap, data), sizeof retval); 315 *retval = SCARG(uap, data); 316 317 goto out; 318 319 break; 320 321 case LINUX_PTRACE_POKEUSR: /* XXX Not tested */ 322 addr = SCARG(uap, addr); 323 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK); 324 error = process_read_regs(t, regs); 325 if (error) 326 goto out; 327 328 PHOLD(t); /* need full process info */ 329 error = 0; 330 if ((addr < LUSR_OFF(lusr_startgdb)) || 331 (addr > LUSR_OFF(lu_comm_end))) 332 error = 1; 333 else if (addr == LUSR_OFF(u_tsize)) 334 error = 1; 335 else if (addr == LUSR_OFF(u_dsize)) 336 error = 1; 337 else if (addr == LUSR_OFF(u_ssize)) 338 error = 1; 339 else if (addr == LUSR_OFF(start_code)) 340 error = 1; 341 else if (addr == LUSR_OFF(start_stack)) 342 error = 1; 343 else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) && 344 (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end))) 345 regs->fixreg[addr / sizeof (register_t)] = 346 (register_t)SCARG(uap, data); 347 else if (addr == LUSR_REG_OFF(lnip)) 348 regs->pc = (register_t)SCARG(uap, data); 349 else if (addr == LUSR_REG_OFF(lctr)) 350 regs->ctr = (register_t)SCARG(uap, data); 351 else if (addr == LUSR_REG_OFF(llink)) 352 regs->lr = (register_t)SCARG(uap, data); 353 else if (addr == LUSR_REG_OFF(lxer)) 354 regs->xer = (register_t)SCARG(uap, data); 355 else if (addr == LUSR_REG_OFF(lccr)) 356 regs->cr = (register_t)SCARG(uap, data); 357 else if (addr == LUSR_OFF(signal)) 358 error = 1; 359 else if (addr == LUSR_OFF(magic)) 360 error = 1; 361 else if (addr == LUSR_OFF(u_comm)) 362 error = 1; 363 else { 364 #ifdef DEBUG_LINUX 365 printf("linux_ptrace: unsupported address: %d\n", addr); 366 #endif 367 error = 1; 368 } 369 370 PRELE(t); 371 372 error = process_write_regs(t,regs); 373 if (error) 374 goto out; 375 376 break; 377 default: 378 /* never reached */ 379 break; 380 } 381 return EIO; 382 383 out: 384 if (regs) 385 FREE(regs, M_TEMP); 386 if (fpregs) 387 FREE(fpregs, M_TEMP); 388 if (linux_regs) 389 FREE(linux_regs, M_TEMP); 390 if (linux_fpreg) 391 FREE(linux_fpreg, M_TEMP); 392 return (error); 393 } 394