Home | History | Annotate | Line # | Download | only in arm
linux_ptrace.c revision 1.12
      1  1.12        ad /*	$NetBSD: linux_ptrace.c,v 1.12 2008/04/23 14:18:50 ad Exp $	*/
      2   1.2     bjh21 
      3   1.2     bjh21 /*-
      4   1.2     bjh21  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.2     bjh21  * All rights reserved.
      6   1.2     bjh21  *
      7   1.2     bjh21  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2     bjh21  * by Matthias Scheler.
      9   1.2     bjh21  *
     10   1.2     bjh21  * Redistribution and use in source and binary forms, with or without
     11   1.2     bjh21  * modification, are permitted provided that the following conditions
     12   1.2     bjh21  * are met:
     13   1.2     bjh21  * 1. Redistributions of source code must retain the above copyright
     14   1.2     bjh21  *    notice, this list of conditions and the following disclaimer.
     15   1.2     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2     bjh21  *    notice, this list of conditions and the following disclaimer in the
     17   1.2     bjh21  *    documentation and/or other materials provided with the distribution.
     18   1.2     bjh21  * 3. All advertising materials mentioning features or use of this software
     19   1.2     bjh21  *    must display the following acknowledgement:
     20   1.2     bjh21  *	This product includes software developed by the NetBSD
     21   1.2     bjh21  *	Foundation, Inc. and its contributors.
     22   1.2     bjh21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.2     bjh21  *    contributors may be used to endorse or promote products derived
     24   1.2     bjh21  *    from this software without specific prior written permission.
     25   1.2     bjh21  *
     26   1.2     bjh21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.2     bjh21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.2     bjh21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.2     bjh21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.2     bjh21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.2     bjh21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.2     bjh21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.2     bjh21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.2     bjh21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.2     bjh21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.2     bjh21  * POSSIBILITY OF SUCH DAMAGE.
     37   1.2     bjh21  */
     38   1.2     bjh21 
     39   1.2     bjh21 
     40   1.2     bjh21 #include <sys/cdefs.h>
     41  1.12        ad __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.12 2008/04/23 14:18:50 ad Exp $");
     42   1.1     bjh21 
     43   1.1     bjh21 #include <sys/param.h>
     44   1.2     bjh21 #include <sys/malloc.h>
     45   1.2     bjh21 #include <sys/mount.h>
     46   1.2     bjh21 #include <sys/proc.h>
     47   1.2     bjh21 #include <sys/ptrace.h>
     48   1.2     bjh21 #include <sys/systm.h>
     49   1.2     bjh21 #include <sys/syscallargs.h>
     50   1.2     bjh21 #include <uvm/uvm_extern.h>
     51   1.2     bjh21 
     52   1.2     bjh21 #include <machine/reg.h>
     53   1.2     bjh21 
     54   1.2     bjh21 #include <compat/linux/common/linux_types.h>
     55   1.2     bjh21 #include <compat/linux/common/linux_ptrace.h>
     56   1.2     bjh21 #include <compat/linux/common/linux_signal.h>
     57   1.2     bjh21 
     58   1.2     bjh21 #include <compat/linux/common/linux_util.h>
     59   1.2     bjh21 #include <compat/linux/common/linux_machdep.h>
     60   1.2     bjh21 #include <compat/linux/common/linux_emuldata.h>
     61   1.2     bjh21 #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
     62   1.2     bjh21 
     63   1.2     bjh21 #include <compat/linux/linux_syscallargs.h>
     64   1.1     bjh21 
     65   1.2     bjh21 #include <lib/libkern/libkern.h>	/* for offsetof() */
     66   1.1     bjh21 
     67   1.2     bjh21 /*
     68   1.2     bjh21  * On ARMv2, uregs contains R0--R15, orig_R0.
     69   1.2     bjh21  * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
     70   1.2     bjh21  * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
     71   1.2     bjh21  * just produce the ARMv3 version.
     72   1.2     bjh21  */
     73   1.2     bjh21 
     74   1.2     bjh21 struct linux_reg {
     75   1.2     bjh21 	long uregs[18];
     76   1.2     bjh21 };
     77   1.2     bjh21 
     78   1.2     bjh21 #define LINUX_REG_R0	0
     79   1.2     bjh21 #define LINUX_REG_R1	1
     80   1.2     bjh21 #define LINUX_REG_R2	2
     81   1.2     bjh21 #define LINUX_REG_R3	3
     82   1.2     bjh21 #define LINUX_REG_R4	4
     83   1.2     bjh21 #define LINUX_REG_R5	5
     84   1.2     bjh21 #define LINUX_REG_R6	6
     85   1.2     bjh21 #define LINUX_REG_R7	7
     86   1.2     bjh21 #define LINUX_REG_R8	8
     87   1.2     bjh21 #define LINUX_REG_R9	9
     88   1.2     bjh21 #define LINUX_REG_R10	10
     89   1.2     bjh21 #define LINUX_REG_FP	11
     90   1.2     bjh21 #define LINUX_REG_IP	12
     91   1.2     bjh21 #define LINUX_REG_SP	13
     92   1.2     bjh21 #define LINUX_REG_LR	14
     93   1.2     bjh21 #define LINUX_REG_PC	15
     94   1.2     bjh21 #define LINUX_REG_CPSR	16
     95   1.2     bjh21 #define LINUX_REG_ORIG_R0 17
     96   1.2     bjh21 
     97  1.12        ad int linux_ptrace_disabled = 1;	/* bitrotted */
     98  1.12        ad 
     99   1.1     bjh21 int
    100  1.11       dsl linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
    101   1.1     bjh21 {
    102  1.11       dsl 	/* {
    103   1.2     bjh21 		syscallarg(int) request;
    104   1.2     bjh21 		syscallarg(int) pid;
    105   1.2     bjh21 		syscallarg(int) addr;
    106   1.2     bjh21 		syscallarg(int) data;
    107  1.11       dsl 	} */
    108   1.3   thorpej 	struct proc *p = l->l_proc;
    109   1.2     bjh21 	int request, error;
    110   1.2     bjh21 	struct proc *t;				/* target process */
    111   1.3   thorpej 	struct lwp *lt;
    112   1.2     bjh21 	struct reg *regs = NULL;
    113   1.2     bjh21 	struct fpreg *fpregs = NULL;
    114   1.2     bjh21 	struct linux_reg *linux_regs = NULL;
    115   1.2     bjh21 	struct linux_fpreg *linux_fpregs = NULL;
    116   1.2     bjh21 
    117  1.12        ad 	if (linux_ptrace_disabled)
    118  1.12        ad 		return ENOSYS;
    119  1.12        ad 
    120   1.2     bjh21 	request = SCARG(uap, request);
    121   1.2     bjh21 
    122   1.2     bjh21 	if ((request != LINUX_PTRACE_GETREGS) &&
    123   1.2     bjh21 	    (request != LINUX_PTRACE_SETREGS))
    124   1.2     bjh21 		return EIO;
    125   1.2     bjh21 
    126   1.2     bjh21 	/* Find the process we're supposed to be operating on. */
    127   1.2     bjh21 	if ((t = pfind(SCARG(uap, pid))) == NULL)
    128   1.2     bjh21 		return ESRCH;
    129   1.2     bjh21 
    130   1.2     bjh21 	/*
    131   1.2     bjh21 	 * You can't do what you want to the process if:
    132   1.2     bjh21 	 *	(1) It's not being traced at all,
    133   1.2     bjh21 	 */
    134   1.8        ad 	if (!ISSET(t->p_slflag, PSL_TRACED))
    135   1.2     bjh21 		return EPERM;
    136   1.2     bjh21 
    137   1.2     bjh21 	/*
    138   1.2     bjh21 	 *	(2) it's being traced by procfs (which has
    139   1.2     bjh21 	 *	    different signal delivery semantics),
    140   1.2     bjh21 	 */
    141   1.8        ad 	if (ISSET(t->p_slflag, PSL_FSTRACE))
    142   1.2     bjh21 		return EBUSY;
    143   1.2     bjh21 
    144   1.2     bjh21 	/*
    145   1.2     bjh21 	 *	(3) it's not being traced by _you_, or
    146   1.2     bjh21 	 */
    147   1.2     bjh21 	if (t->p_pptr != p)
    148   1.2     bjh21 		return EBUSY;
    149   1.2     bjh21 
    150   1.2     bjh21 	/*
    151   1.2     bjh21 	 *	(4) it's not currently stopped.
    152   1.2     bjh21 	 */
    153   1.8        ad 	if (t->p_stat != SSTOP || !t->p_waited)
    154   1.2     bjh21 		return EBUSY;
    155   1.2     bjh21 
    156   1.3   thorpej 	/* XXX NJWLWP
    157   1.3   thorpej 	 * The entire ptrace interface needs work to be useful to
    158   1.3   thorpej 	 * a process with multiple LWPs. For the moment, we'll
    159   1.3   thorpej 	 * just kluge this and fail on others.
    160   1.3   thorpej 	 */
    161   1.3   thorpej 
    162   1.3   thorpej 	if (p->p_nlwps > 1)
    163   1.3   thorpej 		return (ENOSYS);
    164   1.3   thorpej 
    165   1.3   thorpej 	lt = LIST_FIRST(&t->p_lwps);
    166   1.3   thorpej 
    167   1.2     bjh21 	*retval = 0;
    168   1.2     bjh21 
    169   1.2     bjh21 	switch (request) {
    170   1.2     bjh21 	case  LINUX_PTRACE_GETREGS:
    171   1.2     bjh21 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
    172   1.2     bjh21 		MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg),
    173   1.2     bjh21 			M_TEMP, M_WAITOK);
    174   1.2     bjh21 
    175   1.3   thorpej 		error = process_read_regs(lt, regs);
    176   1.2     bjh21 		if (error != 0)
    177   1.2     bjh21 			goto out;
    178   1.2     bjh21 
    179   1.2     bjh21 		memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
    180   1.2     bjh21 		linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
    181   1.2     bjh21 		linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
    182   1.2     bjh21 		linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
    183   1.2     bjh21 		linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
    184   1.2     bjh21 		linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];
    185   1.2     bjh21 
    186   1.9  christos 		error = copyout(linux_regs, (void *)SCARG(uap, data),
    187   1.2     bjh21 		    sizeof(struct linux_reg));
    188   1.2     bjh21 		goto out;
    189   1.2     bjh21 
    190   1.2     bjh21 	case  LINUX_PTRACE_SETREGS:
    191   1.2     bjh21 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
    192   1.2     bjh21 		MALLOC(linux_regs, struct linux_reg *, sizeof(struct linux_reg),
    193   1.2     bjh21 			M_TEMP, M_WAITOK);
    194   1.2     bjh21 
    195   1.9  christos 		error = copyin((void *)SCARG(uap, data), linux_regs,
    196   1.2     bjh21 		    sizeof(struct linux_reg));
    197   1.2     bjh21 		if (error != 0)
    198   1.2     bjh21 			goto out;
    199   1.2     bjh21 
    200   1.2     bjh21 		memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
    201   1.2     bjh21 		regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
    202   1.2     bjh21 		regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
    203   1.2     bjh21 		regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
    204   1.2     bjh21 		regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
    205   1.2     bjh21 
    206   1.3   thorpej 		error = process_write_regs(lt, regs);
    207   1.2     bjh21 		goto out;
    208   1.2     bjh21 
    209   1.2     bjh21 	default:
    210   1.2     bjh21 		/* never reached */
    211   1.2     bjh21 		break;
    212   1.2     bjh21 	}
    213   1.2     bjh21 
    214   1.2     bjh21 	return EIO;
    215   1.2     bjh21 
    216   1.2     bjh21     out:
    217   1.2     bjh21 	if (regs)
    218   1.2     bjh21 		FREE(regs, M_TEMP);
    219   1.2     bjh21 	if (fpregs)
    220   1.2     bjh21 		FREE(fpregs, M_TEMP);
    221   1.2     bjh21 	if (linux_regs)
    222   1.2     bjh21 		FREE(linux_regs, M_TEMP);
    223   1.2     bjh21 	if (linux_fpregs)
    224   1.2     bjh21 		FREE(linux_fpregs, M_TEMP);
    225   1.2     bjh21 	return (error);
    226   1.1     bjh21 
    227   1.1     bjh21 }
    228