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