Home | History | Annotate | Line # | Download | only in arm
linux_ptrace.c revision 1.5
      1 /*	$NetBSD: linux_ptrace.c,v 1.5 2006/08/30 11:14:39 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 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.5 2006/08/30 11:14:39 matt Exp $");
     42 #include "opt_ptrace.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/malloc.h>
     46 #include <sys/mount.h>
     47 #include <sys/proc.h>
     48 #include <sys/ptrace.h>
     49 #include <sys/systm.h>
     50 #include <sys/sa.h>
     51 #include <sys/syscallargs.h>
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <machine/reg.h>
     55 
     56 #include <compat/linux/common/linux_types.h>
     57 #include <compat/linux/common/linux_ptrace.h>
     58 #include <compat/linux/common/linux_signal.h>
     59 
     60 #include <compat/linux/common/linux_util.h>
     61 #include <compat/linux/common/linux_machdep.h>
     62 #include <compat/linux/common/linux_emuldata.h>
     63 #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
     64 
     65 #include <compat/linux/linux_syscallargs.h>
     66 
     67 #include <lib/libkern/libkern.h>	/* for offsetof() */
     68 
     69 /*
     70  * On ARMv2, uregs contains R0--R15, orig_R0.
     71  * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
     72  * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
     73  * just produce the ARMv3 version.
     74  */
     75 
     76 struct linux_reg {
     77 	long uregs[18];
     78 };
     79 
     80 #define LINUX_REG_R0	0
     81 #define LINUX_REG_R1	1
     82 #define LINUX_REG_R2	2
     83 #define LINUX_REG_R3	3
     84 #define LINUX_REG_R4	4
     85 #define LINUX_REG_R5	5
     86 #define LINUX_REG_R6	6
     87 #define LINUX_REG_R7	7
     88 #define LINUX_REG_R8	8
     89 #define LINUX_REG_R9	9
     90 #define LINUX_REG_R10	10
     91 #define LINUX_REG_FP	11
     92 #define LINUX_REG_IP	12
     93 #define LINUX_REG_SP	13
     94 #define LINUX_REG_LR	14
     95 #define LINUX_REG_PC	15
     96 #define LINUX_REG_CPSR	16
     97 #define LINUX_REG_ORIG_R0 17
     98 
     99 int
    100 linux_sys_ptrace_arch(l, v, retval)
    101 	struct lwp *l;
    102 	void *v;
    103 	register_t *retval;
    104 {
    105 	struct linux_sys_ptrace_args /* {
    106 		syscallarg(int) request;
    107 		syscallarg(int) pid;
    108 		syscallarg(int) addr;
    109 		syscallarg(int) data;
    110 	} */ *uap = v;
    111 	struct proc *p = l->l_proc;
    112 	int request, error;
    113 	struct proc *t;				/* target process */
    114 	struct lwp *lt;
    115 	struct reg *regs = NULL;
    116 	struct fpreg *fpregs = NULL;
    117 	struct linux_reg *linux_regs = NULL;
    118 	struct linux_fpreg *linux_fpregs = NULL;
    119 
    120 	request = SCARG(uap, request);
    121 
    122 	if ((request != LINUX_PTRACE_GETREGS) &&
    123 	    (request != LINUX_PTRACE_SETREGS))
    124 		return EIO;
    125 
    126 	/* Find the process we're supposed to be operating on. */
    127 	if ((t = pfind(SCARG(uap, pid))) == NULL)
    128 		return ESRCH;
    129 
    130 	/*
    131 	 * You can't do what you want to the process if:
    132 	 *	(1) It's not being traced at all,
    133 	 */
    134 	if (!ISSET(t->p_flag, P_TRACED))
    135 		return EPERM;
    136 
    137 	/*
    138 	 *	(2) it's being traced by procfs (which has
    139 	 *	    different signal delivery semantics),
    140 	 */
    141 	if (ISSET(t->p_flag, P_FSTRACE))
    142 		return EBUSY;
    143 
    144 	/*
    145 	 *	(3) it's not being traced by _you_, or
    146 	 */
    147 	if (t->p_pptr != p)
    148 		return EBUSY;
    149 
    150 	/*
    151 	 *	(4) it's not currently stopped.
    152 	 */
    153 	if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
    154 		return EBUSY;
    155 
    156 	/* XXX NJWLWP
    157 	 * The entire ptrace interface needs work to be useful to
    158 	 * a process with multiple LWPs. For the moment, we'll
    159 	 * just kluge this and fail on others.
    160 	 */
    161 
    162 	if (p->p_nlwps > 1)
    163 		return (ENOSYS);
    164 
    165 	lt = LIST_FIRST(&t->p_lwps);
    166 
    167 	*retval = 0;
    168 
    169 	switch (request) {
    170 	case  LINUX_PTRACE_GETREGS:
    171 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
    172 		MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg),
    173 			M_TEMP, M_WAITOK);
    174 
    175 		error = process_read_regs(lt, regs);
    176 		if (error != 0)
    177 			goto out;
    178 
    179 		memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
    180 		linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
    181 		linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
    182 		linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
    183 		linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
    184 		linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];
    185 
    186 		error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
    187 		    sizeof(struct linux_reg));
    188 		goto out;
    189 
    190 	case  LINUX_PTRACE_SETREGS:
    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 = copyin((caddr_t)SCARG(uap, data), linux_regs,
    196 		    sizeof(struct linux_reg));
    197 		if (error != 0)
    198 			goto out;
    199 
    200 		memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
    201 		regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
    202 		regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
    203 		regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
    204 		regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
    205 
    206 		error = process_write_regs(lt, regs);
    207 		goto out;
    208 
    209 	default:
    210 		/* never reached */
    211 		break;
    212 	}
    213 
    214 	return EIO;
    215 
    216     out:
    217 	if (regs)
    218 		FREE(regs, M_TEMP);
    219 	if (fpregs)
    220 		FREE(fpregs, M_TEMP);
    221 	if (linux_regs)
    222 		FREE(linux_regs, M_TEMP);
    223 	if (linux_fpregs)
    224 		FREE(linux_fpregs, M_TEMP);
    225 	return (error);
    226 
    227 }
    228