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