1 /* $NetBSD: linux_ptrace.c,v 1.19 2008/04/28 20:23:43 martin 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.19 2008/04/28 20:23:43 martin Exp $"); 34 35 #include "opt_ptrace.h" 36 37 #include <sys/param.h> 38 #include <sys/malloc.h> 39 #include <sys/mount.h> 40 #include <sys/proc.h> 41 #include <sys/ptrace.h> 42 #include <sys/systm.h> 43 #include <sys/syscallargs.h> 44 #include <uvm/uvm_extern.h> 45 46 #include <machine/reg.h> 47 48 #include <compat/linux/common/linux_types.h> 49 #include <compat/linux/common/linux_ptrace.h> 50 #include <compat/linux/common/linux_signal.h> 51 52 #include <compat/linux/common/linux_util.h> 53 #include <compat/linux/common/linux_machdep.h> 54 #include <compat/linux/common/linux_emuldata.h> 55 #include <compat/linux/common/linux_exec.h> /* for emul_linux */ 56 57 #include <compat/linux/linux_syscallargs.h> 58 59 #include <lib/libkern/libkern.h> /* for offsetof() */ 60 61 /* 62 * From Linux's include/asm-ppc/ptrace.h. 63 * structure used for storing reg context: defined in linux_machdep.h 64 * structure used for storing floating point context: 65 * Linux just uses a double fpr[32], no struct 66 */ 67 68 /* 69 * user struct for linux process - this is used for Linux ptrace emulation 70 * most of it is junk only used by gdb 71 * 72 * From Linux's include/asm-ppc/user.h 73 * 74 * XXX u_ar0 was a struct reg in Linux/powerpc 75 * Can't find out what a struct regs is for Linux/powerpc, 76 * so we use a struct pt_regs instead. don't know if this is right. 77 */ 78 79 struct linux_user { 80 struct linux_pt_regs regs; 81 #define lusr_startgdb regs 82 size_t u_tsize; 83 size_t u_dsize; 84 size_t u_ssize; 85 unsigned long start_code; 86 unsigned long start_data; 87 unsigned long start_stack; 88 long int signal; 89 struct linux_pt_regs *u_ar0; /* help gdb find registers */ 90 unsigned long magic; 91 char u_comm[32]; 92 #define lu_comm_end u_comm[31] 93 }; 94 95 #define LUSR_OFF(member) offsetof(struct linux_user, member) 96 #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member) 97 #define ISSET(t, f) ((t) & (f)) 98 99 int linux_ptrace_disabled = 1; /* bitrotted */ 100 101 /* XXX Check me! (From NetBSD/i386) */ 102 int 103 linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval) 104 { 105 /* { 106 syscallarg(int) request; 107 syscallarg(int) pid; 108 syscallarg(int) addr; 109 syscallarg(int) data; 110 } */ 111 int request, error; 112 struct proc *p = l->l_proc; 113 struct proc *t; /* target process */ 114 struct lwp *lt; 115 struct reg *regs = NULL; 116 struct fpreg *fpregs = NULL; 117 struct linux_pt_regs *linux_regs = NULL; 118 double *linux_fpreg = NULL; /* it's an array, not a struct */ 119 int addr; 120 int i; 121 122 if (linux_ptrace_disabled) 123 return ENOSYS; 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_slflag, PSL_TRACED)) /* XXXSMP */ 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_slflag, PSL_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 || !t->p_waited) 165 return EBUSY; 166 167 lt = LIST_FIRST(&t->p_lwps); 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(lt, 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, (void *)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((void *)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(lt, 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(lt, 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, (void *)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((void *)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(lt, 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(lt, regs); 262 if (error) 263 goto out; 264 265 uvm_lwp_hold(lt); /* 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 uvm_lwp_rele(lt); 309 310 if (error) 311 goto out; 312 313 error = copyout (retval, 314 (void *)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(lt, regs); 325 if (error) 326 goto out; 327 328 uvm_lwp_hold(lt); /* 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 uvm_lwp_rele(lt); 371 372 error = process_write_regs(lt,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