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