1 /* $NetBSD: linux_ptrace.c,v 1.15 2006/09/01 21:20:46 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 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. 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.15 2006/09/01 21:20:46 matt 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/syscall.h> 50 #include <sys/syscallargs.h> 51 #include <uvm/uvm_extern.h> 52 53 #include <machine/reg.h> 54 55 #include <compat/linux/common/linux_types.h> 56 #include <compat/linux/common/linux_ptrace.h> 57 #include <compat/linux/common/linux_signal.h> 58 59 #include <compat/linux/common/linux_util.h> 60 #include <compat/linux/common/linux_machdep.h> 61 #include <compat/linux/common/linux_emuldata.h> 62 #include <compat/linux/common/linux_exec.h> /* for emul_linux */ 63 64 #include <compat/linux/linux_syscallargs.h> 65 66 #include <lib/libkern/libkern.h> /* for offsetof() */ 67 68 struct linux_reg { 69 long ebx; 70 long ecx; 71 long edx; 72 long esi; 73 long edi; 74 long ebp; 75 long eax; 76 long xds, xes; /* unsigned short ds, __ds, es, __es; */ 77 long __fs, __gs; /* unsigned short fs, __fs, gs, __gs; */ 78 long orig_eax; 79 long eip; 80 long xcs; /* unsigned short cs, __cs; */ 81 long eflags; 82 long esp; 83 long xss; /* unsigned short ss, __ss; */ 84 }; 85 86 /* structure used for storing floating point context */ 87 struct linux_fpctx { 88 long cwd; 89 long swd; 90 long twd; 91 long fip; 92 long fcs; 93 long foo; 94 long fos; 95 long st_space[20]; 96 }; 97 98 /* user struct for linux process - this is used for Linux ptrace emulation */ 99 /* most of it is junk only used by gdb */ 100 struct linux_user { 101 struct linux_reg regs; /* registers */ 102 int u_fpvalid; /* true if math co-processor being used. */ 103 struct linux_fpctx i387; /* Math Co-processor registers. */ 104 /* The rest of this junk is to help gdb figure out what goes where */ 105 #define lusr_startgdb u_tsize 106 unsigned long int u_tsize; /* Text segment size (pages). */ 107 unsigned long int u_dsize; /* Data segment size (pages). */ 108 unsigned long int u_ssize; /* Stack segment size (pages). */ 109 unsigned long start_code; /* Starting virtual address of text. */ 110 unsigned long start_stack; /* Starting virtual address of stack 111 area. This is actually the bottom of 112 the stack, the top of the stack is 113 always found in the esp register. */ 114 long int __signal; /* Signal that caused the core dump. */ 115 int __reserved; /* unused */ 116 void *u_ar0; /* Used by gdb to help find the values 117 for the registers. */ 118 struct linux_fpctx *u_fpstate; /* Math Co-processor pointer. */ 119 unsigned long __magic; /* To uniquely identify a core file */ 120 char u_comm[32]; /* User command that was responsible */ 121 int u_debugreg[8]; 122 #define u_debugreg_end u_debugreg[7] 123 }; 124 125 #define LUSR_OFF(member) offsetof(struct linux_user, member) 126 #define ISSET(t, f) ((t) & (f)) 127 128 int 129 linux_sys_ptrace_arch(l, v, retval) 130 struct lwp *l; 131 void *v; 132 register_t *retval; 133 { 134 struct linux_sys_ptrace_args /* { 135 syscallarg(int) request; 136 syscallarg(int) pid; 137 syscallarg(int) addr; 138 syscallarg(int) data; 139 } */ *uap = v; 140 struct proc *p = l->l_proc; 141 int request, error; 142 struct proc *t; /* target process */ 143 struct lwp *lt; 144 struct reg *regs = NULL; 145 struct fpreg *fpregs = NULL; 146 struct linux_reg *linux_regs = NULL; 147 struct linux_fpctx *linux_fpregs = NULL; 148 int addr; 149 150 request = SCARG(uap, request); 151 152 if ((request != LINUX_PTRACE_PEEKUSR) && 153 (request != LINUX_PTRACE_POKEUSR) && 154 (request != LINUX_PTRACE_GETREGS) && 155 (request != LINUX_PTRACE_SETREGS) && 156 (request != LINUX_PTRACE_GETFPREGS) && 157 (request != LINUX_PTRACE_SETFPREGS)) 158 return EIO; 159 160 /* Find the process we're supposed to be operating on. */ 161 if ((t = pfind(SCARG(uap, pid))) == NULL) 162 return ESRCH; 163 164 /* 165 * You can't do what you want to the process if: 166 * (1) It's not being traced at all, 167 */ 168 if (!ISSET(t->p_flag, P_TRACED)) 169 return EPERM; 170 171 /* 172 * (2) it's being traced by procfs (which has 173 * different signal delivery semantics), 174 */ 175 if (ISSET(t->p_flag, P_FSTRACE)) 176 return EBUSY; 177 178 /* 179 * (3) it's not being traced by _you_, or 180 */ 181 if (t->p_pptr != p) 182 return EBUSY; 183 184 /* 185 * (4) it's not currently stopped. 186 */ 187 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED)) 188 return EBUSY; 189 190 /* XXX NJWLWP 191 * The entire ptrace interface needs work to be useful to 192 * a process with multiple LWPs. For the moment, we'll 193 * just kluge this and fail on others. 194 */ 195 196 if (p->p_nlwps > 1) 197 return (ENOSYS); 198 199 lt = LIST_FIRST(&t->p_lwps); 200 201 *retval = 0; 202 203 switch (request) { 204 case LINUX_PTRACE_GETREGS: 205 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK); 206 MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg), 207 M_TEMP, M_WAITOK); 208 209 error = process_read_regs(lt, regs); 210 if (error != 0) 211 goto out; 212 213 linux_regs->ebx = regs->r_ebx; 214 linux_regs->ecx = regs->r_ecx; 215 linux_regs->edx = regs->r_edx; 216 linux_regs->esi = regs->r_esi; 217 linux_regs->edi = regs->r_edi; 218 linux_regs->ebp = regs->r_ebp; 219 linux_regs->eax = regs->r_eax; 220 linux_regs->xds = regs->r_ds; 221 linux_regs->xes = regs->r_es; 222 linux_regs->orig_eax = regs->r_eax; /* XXX is this correct? */ 223 linux_regs->eip = regs->r_cs + regs->r_eip; 224 linux_regs->xcs = regs->r_cs; 225 linux_regs->eflags = regs->r_eflags; 226 linux_regs->esp = regs->r_esp; 227 linux_regs->xss = regs->r_ss; 228 229 error = copyout(linux_regs, (caddr_t)SCARG(uap, data), 230 sizeof(struct linux_reg)); 231 goto out; 232 233 case LINUX_PTRACE_SETREGS: 234 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK); 235 MALLOC(linux_regs, struct linux_reg *, sizeof(struct linux_reg), 236 M_TEMP, M_WAITOK); 237 238 error = copyin((caddr_t)SCARG(uap, data), linux_regs, 239 sizeof(struct linux_reg)); 240 if (error != 0) 241 goto out; 242 243 regs->r_ebx = linux_regs->ebx; 244 regs->r_ecx = linux_regs->ecx; 245 regs->r_edx = linux_regs->edx; 246 regs->r_esi = linux_regs->esi; 247 regs->r_edi = linux_regs->edi; 248 regs->r_ebp = linux_regs->ebp; 249 regs->r_eax = linux_regs->eax; 250 regs->r_ds = linux_regs->xds; 251 regs->r_es = linux_regs->xes; 252 regs->r_eip = linux_regs->eip - linux_regs->xcs; 253 regs->r_cs = linux_regs->xcs; 254 regs->r_eflags = linux_regs->eflags; 255 regs->r_esp = linux_regs->esp; 256 regs->r_ss = linux_regs->xss; 257 258 error = process_write_regs(lt, regs); 259 goto out; 260 261 case LINUX_PTRACE_GETFPREGS: 262 MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg), 263 M_TEMP, M_WAITOK); 264 MALLOC(linux_fpregs, struct linux_fpctx *, 265 sizeof(struct linux_fpctx), M_TEMP, M_WAITOK); 266 267 error = process_read_fpregs(lt, fpregs); 268 if (error != 0) 269 goto out; 270 271 /* zero the contents if NetBSD fpreg structure is smaller */ 272 if (sizeof(struct fpreg) < sizeof(struct linux_fpctx)) 273 memset(linux_fpregs, '\0', sizeof(struct linux_fpctx)); 274 275 memcpy(linux_fpregs, fpregs, 276 min(sizeof(struct linux_fpctx), sizeof(struct fpreg))); 277 error = copyout(linux_fpregs, (caddr_t)SCARG(uap, data), 278 sizeof(struct linux_fpctx)); 279 goto out; 280 281 case LINUX_PTRACE_SETFPREGS: 282 MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg), 283 M_TEMP, M_WAITOK); 284 MALLOC(linux_fpregs, struct linux_fpctx *, 285 sizeof(struct linux_fpctx), M_TEMP, M_WAITOK); 286 error = copyin((caddr_t)SCARG(uap, data), linux_fpregs, 287 sizeof(struct linux_fpctx)); 288 if (error != 0) 289 goto out; 290 291 memset(fpregs, '\0', sizeof(struct fpreg)); 292 memcpy(fpregs, linux_fpregs, 293 min(sizeof(struct linux_fpctx), sizeof(struct fpreg))); 294 295 error = process_write_regs(lt, regs); 296 goto out; 297 298 case LINUX_PTRACE_PEEKUSR: 299 addr = SCARG(uap, addr); 300 301 PHOLD(lt); /* need full process info */ 302 error = 0; 303 if (addr < LUSR_OFF(lusr_startgdb)) { 304 /* XXX should provide appropriate register */ 305 error = 1; 306 } else if (addr == LUSR_OFF(u_tsize)) 307 *retval = p->p_vmspace->vm_tsize; 308 else if (addr == LUSR_OFF(u_dsize)) 309 *retval = p->p_vmspace->vm_dsize; 310 else if (addr == LUSR_OFF(u_ssize)) 311 *retval = p->p_vmspace->vm_ssize; 312 else if (addr == LUSR_OFF(start_code)) 313 *retval = (register_t) p->p_vmspace->vm_taddr; 314 else if (addr == LUSR_OFF(start_stack)) 315 *retval = (register_t) p->p_vmspace->vm_minsaddr; 316 else if (addr == LUSR_OFF(u_ar0)) 317 *retval = LUSR_OFF(regs); 318 else if (addr >= LUSR_OFF(u_debugreg) 319 && addr <= LUSR_OFF(u_debugreg_end)) { 320 int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int); 321 322 /* only do this for Linux processes */ 323 if (t->p_emul != &emul_linux) 324 error = EINVAL; 325 else { 326 *retval = ((struct linux_emuldata *) 327 t->p_emuldata)->debugreg[off]; 328 } 329 } else if (addr == LUSR_OFF(__signal)) { 330 error = 1; 331 } else if (addr == LUSR_OFF(__signal)) { 332 error = 1; 333 } else if (addr == LUSR_OFF(u_fpstate)) { 334 error = 1; 335 } else if (addr == LUSR_OFF(__magic)) { 336 error = 1; 337 } else if (addr == LUSR_OFF(u_comm)) { 338 error = 1; 339 } else { 340 #ifdef DEBUG_LINUX 341 printf("linux_ptrace: unsupported address: %d\n", addr); 342 #endif 343 error = 1; 344 } 345 346 PRELE(lt); 347 348 if (!error) 349 return 0; 350 351 case LINUX_PTRACE_POKEUSR: 352 /* we only support setting debugregs for now */ 353 addr = SCARG(uap, addr); 354 if (addr >= LUSR_OFF(u_debugreg) 355 && addr <= LUSR_OFF(u_debugreg_end)) { 356 int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int); 357 int data = SCARG(uap, data); 358 359 /* only do this for Linux processes */ 360 if (t->p_emul != &emul_linux) 361 return EINVAL; 362 363 PHOLD(lt); 364 ((struct linux_emuldata *)t->p_emuldata)->debugreg[off] = data; 365 PRELE(lt); 366 return (0); 367 } 368 369 break; 370 default: 371 /* never reached */ 372 break; 373 } 374 375 return EIO; 376 377 out: 378 if (regs) 379 FREE(regs, M_TEMP); 380 if (fpregs) 381 FREE(fpregs, M_TEMP); 382 if (linux_regs) 383 FREE(linux_regs, M_TEMP); 384 if (linux_fpregs) 385 FREE(linux_fpregs, M_TEMP); 386 return (error); 387 } 388