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