1 /* $NetBSD: linux_ptrace.c,v 1.16 2010/07/07 01:30:33 chs 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 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.16 2010/07/07 01:30:33 chs Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/malloc.h> 38 #include <sys/mount.h> 39 #include <sys/proc.h> 40 #include <sys/ptrace.h> 41 #include <sys/systm.h> 42 #include <sys/syscallargs.h> 43 #include <uvm/uvm_extern.h> 44 45 #include <machine/reg.h> 46 47 #include <compat/linux/common/linux_types.h> 48 #include <compat/linux/common/linux_ptrace.h> 49 #include <compat/linux/common/linux_signal.h> 50 51 #include <compat/linux/common/linux_util.h> 52 #include <compat/linux/common/linux_machdep.h> 53 #include <compat/linux/common/linux_emuldata.h> 54 #include <compat/linux/common/linux_exec.h> /* for emul_linux */ 55 56 #include <compat/linux/linux_syscallargs.h> 57 58 #include <lib/libkern/libkern.h> /* for offsetof() */ 59 60 /* 61 * On ARMv2, uregs contains R0--R15, orig_R0. 62 * On ARMv3 and later, it's R0--R15, CPSR, orig_R0. 63 * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we 64 * just produce the ARMv3 version. 65 */ 66 67 struct linux_reg { 68 long uregs[18]; 69 }; 70 71 #define LINUX_REG_R0 0 72 #define LINUX_REG_R1 1 73 #define LINUX_REG_R2 2 74 #define LINUX_REG_R3 3 75 #define LINUX_REG_R4 4 76 #define LINUX_REG_R5 5 77 #define LINUX_REG_R6 6 78 #define LINUX_REG_R7 7 79 #define LINUX_REG_R8 8 80 #define LINUX_REG_R9 9 81 #define LINUX_REG_R10 10 82 #define LINUX_REG_FP 11 83 #define LINUX_REG_IP 12 84 #define LINUX_REG_SP 13 85 #define LINUX_REG_LR 14 86 #define LINUX_REG_PC 15 87 #define LINUX_REG_CPSR 16 88 #define LINUX_REG_ORIG_R0 17 89 90 int linux_ptrace_disabled = 1; /* bitrotted */ 91 92 int 93 linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap, 94 register_t *retval) 95 { 96 /* { 97 syscallarg(int) request; 98 syscallarg(int) pid; 99 syscallarg(int) addr; 100 syscallarg(int) data; 101 } */ 102 struct proc *p = l->l_proc, *t; 103 struct lwp *lt; 104 #ifdef _ARM_ARCH_6 105 struct pcb *pcb; 106 void *val; 107 #endif 108 struct reg *regs = NULL; 109 struct linux_reg *linux_regs = NULL; 110 int request, error; 111 112 if (linux_ptrace_disabled) 113 return ENOSYS; 114 115 error = 0; 116 request = SCARG(uap, request); 117 regs = kmem_alloc(sizeof(struct reg), KM_SLEEP); 118 linux_regs = kmem_alloc(sizeof(struct linux_reg), KM_SLEEP); 119 120 switch (request) { 121 case LINUX_PTRACE_GETREGS: 122 break; 123 case LINUX_PTRACE_SETREGS: 124 error = copyin((void *)SCARG(uap, data), linux_regs, 125 sizeof(struct linux_reg)); 126 if (error) { 127 goto out; 128 } 129 break; 130 default: 131 error = EIO; 132 goto out; 133 } 134 135 /* Find the process we are supposed to be operating on. */ 136 mutex_enter(proc_lock); 137 if ((t = proc_find(SCARG(uap, pid))) == NULL) { 138 mutex_exit(proc_lock); 139 error = ESRCH; 140 goto out; 141 } 142 mutex_enter(t->p_lock); 143 mutex_exit(proc_lock); 144 145 /* 146 * You cannot do what you want to the process if: 147 * 1. It is not being traced at all, 148 */ 149 if (!ISSET(t->p_slflag, PSL_TRACED)) { 150 mutex_exit(t->p_lock); 151 error = EPERM; 152 goto out; 153 } 154 /* 155 * 2. It is being traced by procfs (which has different signal 156 * delivery semantics), 157 * 3. It is not being traced by _you_, or 158 * 4. It is not currently stopped. 159 */ 160 if (ISSET(t->p_slflag, PSL_FSTRACE) || t->p_pptr != p || 161 t->p_stat != SSTOP || !t->p_waited) { 162 mutex_exit(t->p_lock); 163 error = EBUSY; 164 goto out; 165 } 166 /* XXX: ptrace needs revamp for multi-threading support. */ 167 if (t->p_nlwps > 1) { 168 mutex_exit(t->p_lock); 169 error = ENOSYS; 170 goto out; 171 } 172 lt = LIST_FIRST(&t->p_lwps); 173 *retval = 0; 174 175 switch (request) { 176 case LINUX_PTRACE_GETREGS: 177 error = process_read_regs(lt, regs); 178 mutex_exit(t->p_lock); 179 if (error) { 180 break; 181 } 182 memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t)); 183 linux_regs->uregs[LINUX_REG_SP] = regs->r_sp; 184 linux_regs->uregs[LINUX_REG_LR] = regs->r_lr; 185 linux_regs->uregs[LINUX_REG_PC] = regs->r_pc; 186 linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr; 187 linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0]; 188 189 error = copyout(linux_regs, (void *)SCARG(uap, data), 190 sizeof(struct linux_reg)); 191 break; 192 193 case LINUX_PTRACE_SETREGS: 194 memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t)); 195 regs->r_sp = linux_regs->uregs[LINUX_REG_SP]; 196 regs->r_lr = linux_regs->uregs[LINUX_REG_LR]; 197 regs->r_pc = linux_regs->uregs[LINUX_REG_PC]; 198 regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR]; 199 error = process_write_regs(lt, regs); 200 mutex_exit(t->p_lock); 201 break; 202 203 #ifdef _ARM_ARCH_6 204 #define LINUX_PTRACE_GET_THREAD_AREA 22 205 case LINUX_PTRACE_GET_THREAD_AREA: 206 mutex_exit(t->p_lock); 207 pcb = lwp_getpcb(l); 208 val = (void *)pcb->pcb_un.un_32.pcb32_user_pid_ro; 209 error = copyout(&val, (void *)SCARG(uap, data), sizeof(val)); 210 break; 211 #endif 212 213 default: 214 mutex_exit(t->p_lock); 215 } 216 out: 217 if (regs) 218 kmem_free(regs, sizeof(*regs)); 219 if (linux_regs) 220 kmem_free(linux_regs, sizeof(*linux_regs)); 221 return error; 222 223 } 224