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