Home | History | Annotate | Line # | Download | only in powerpc
linux_ptrace.c revision 1.16
      1  1.16        ad /*	$NetBSD: linux_ptrace.c,v 1.16 2007/07/09 21:10:46 ad Exp $ */
      2   1.1      manu 
      3   1.1      manu /*-
      4   1.1      manu  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
      5   1.1      manu  * All rights reserved.
      6   1.1      manu  *
      7   1.1      manu  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      manu  * by Matthias Scheler and Emmanuel Dreyfus.
      9   1.1      manu  *
     10   1.1      manu  * Redistribution and use in source and binary forms, with or without
     11   1.1      manu  * modification, are permitted provided that the following conditions
     12   1.1      manu  * are met:
     13   1.1      manu  * 1. Redistributions of source code must retain the above copyright
     14   1.1      manu  *    notice, this list of conditions and the following disclaimer.
     15   1.1      manu  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      manu  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      manu  *    documentation and/or other materials provided with the distribution.
     18   1.1      manu  * 3. All advertising materials mentioning features or use of this software
     19   1.1      manu  *    must display the following acknowledgement:
     20   1.1      manu  *	This product includes software developed by the NetBSD
     21   1.1      manu  *	Foundation, Inc. and its contributors.
     22   1.1      manu  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      manu  *    contributors may be used to endorse or promote products derived
     24   1.1      manu  *    from this software without specific prior written permission.
     25   1.1      manu  *
     26   1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      manu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      manu  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      manu  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      manu  */
     38   1.8     lukem 
     39   1.8     lukem #include <sys/cdefs.h>
     40  1.16        ad __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.16 2007/07/09 21:10:46 ad Exp $");
     41  1.13      matt 
     42  1.13      matt #include "opt_ptrace.h"
     43   1.1      manu 
     44   1.1      manu #include <sys/param.h>
     45   1.1      manu #include <sys/malloc.h>
     46   1.1      manu #include <sys/mount.h>
     47   1.1      manu #include <sys/proc.h>
     48   1.1      manu #include <sys/ptrace.h>
     49   1.1      manu #include <sys/systm.h>
     50   1.1      manu #include <sys/syscallargs.h>
     51   1.1      manu #include <uvm/uvm_extern.h>
     52   1.1      manu 
     53   1.1      manu #include <machine/reg.h>
     54   1.1      manu 
     55   1.1      manu #include <compat/linux/common/linux_types.h>
     56   1.1      manu #include <compat/linux/common/linux_ptrace.h>
     57   1.1      manu #include <compat/linux/common/linux_signal.h>
     58   1.1      manu 
     59   1.1      manu #include <compat/linux/common/linux_util.h>
     60   1.1      manu #include <compat/linux/common/linux_machdep.h>
     61   1.1      manu #include <compat/linux/common/linux_emuldata.h>
     62   1.1      manu #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
     63   1.1      manu 
     64   1.1      manu #include <compat/linux/linux_syscallargs.h>
     65   1.1      manu 
     66   1.1      manu #include <lib/libkern/libkern.h>	/* for offsetof() */
     67   1.1      manu 
     68  1.11     perry /*
     69  1.11     perry  * From Linux's include/asm-ppc/ptrace.h.
     70  1.11     perry  * structure used for storing reg context:  defined in linux_machdep.h
     71   1.1      manu  * structure used for storing floating point context:
     72  1.11     perry  * Linux just uses a double fpr[32], no struct
     73   1.1      manu  */
     74  1.11     perry 
     75  1.11     perry /*
     76  1.11     perry  * user struct for linux process - this is used for Linux ptrace emulation
     77  1.11     perry  * most of it is junk only used by gdb
     78  1.11     perry  *
     79  1.11     perry  * From Linux's include/asm-ppc/user.h
     80  1.11     perry  *
     81  1.11     perry  * XXX u_ar0 was a struct reg in Linux/powerpc
     82  1.11     perry  * Can't find out what a struct regs is for Linux/powerpc,
     83   1.1      manu  * so we use a struct pt_regs instead. don't know if this is right.
     84   1.1      manu  */
     85   1.1      manu 
     86   1.1      manu struct linux_user {
     87  1.11     perry 	struct linux_pt_regs regs;
     88   1.4      manu #define lusr_startgdb regs
     89  1.11     perry 	size_t u_tsize;
     90  1.11     perry 	size_t u_dsize;
     91  1.11     perry 	size_t u_ssize;
     92   1.1      manu 	unsigned long start_code;
     93   1.1      manu 	unsigned long start_data;
     94   1.1      manu 	unsigned long start_stack;
     95  1.11     perry 	long int signal;
     96   1.3  christos 	struct linux_pt_regs *u_ar0;		/* help gdb find registers */
     97  1.11     perry 	unsigned long magic;
     98  1.11     perry 	char u_comm[32];
     99   1.4      manu #define lu_comm_end u_comm[31]
    100   1.1      manu };
    101   1.1      manu 
    102   1.1      manu #define LUSR_OFF(member) offsetof(struct linux_user, member)
    103   1.5      manu #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member)
    104   1.1      manu #define ISSET(t, f) ((t) & (f))
    105   1.1      manu 
    106   1.1      manu int
    107  1.10   thorpej linux_sys_ptrace_arch(l, v, retval)	/* XXX Check me! (From NetBSD/i386) */
    108  1.10   thorpej 	struct lwp *l;
    109   1.1      manu 	void *v;
    110   1.1      manu 	register_t *retval;
    111   1.1      manu {
    112   1.1      manu 	struct linux_sys_ptrace_args /* {
    113   1.1      manu 		syscallarg(int) request;
    114   1.1      manu 		syscallarg(int) pid;
    115   1.1      manu 		syscallarg(int) addr;
    116   1.1      manu 		syscallarg(int) data;
    117   1.1      manu 	} */ *uap = v;
    118   1.1      manu 	int request, error;
    119  1.10   thorpej 	struct proc *p = l->l_proc;
    120   1.1      manu 	struct proc *t;				/* target process */
    121  1.10   thorpej 	struct lwp *lt;
    122   1.1      manu 	struct reg *regs = NULL;
    123   1.1      manu 	struct fpreg *fpregs = NULL;
    124   1.1      manu 	struct linux_pt_regs *linux_regs = NULL;
    125   1.3  christos 	double *linux_fpreg = NULL;  /* it's an array, not a struct */
    126   1.1      manu 	int addr;
    127   1.1      manu 	int i;
    128   1.1      manu 
    129   1.3  christos 	switch (request = SCARG(uap, request)) {
    130   1.5      manu 		case LINUX_PTRACE_PEEKUSR:
    131   1.5      manu 		case LINUX_PTRACE_POKEUSR:
    132   1.5      manu 		case LINUX_PTRACE_GETREGS:
    133   1.5      manu 		case LINUX_PTRACE_SETREGS:
    134   1.5      manu 		case LINUX_PTRACE_GETFPREGS:
    135   1.5      manu 		case LINUX_PTRACE_SETFPREGS:
    136   1.5      manu 			break;
    137   1.5      manu 		default:
    138   1.5      manu 			return EIO;
    139   1.3  christos 	}
    140   1.1      manu 
    141   1.1      manu 	/* Find the process we're supposed to be operating on. */
    142   1.1      manu 	if ((t = pfind(SCARG(uap, pid))) == NULL)
    143   1.1      manu 		return ESRCH;
    144   1.1      manu 
    145   1.1      manu 	/*
    146   1.1      manu 	 * You can't do what you want to the process if:
    147   1.1      manu 	 *	(1) It's not being traced at all,
    148   1.1      manu 	 */
    149  1.14        ad 	if (!ISSET(t->p_slflag, PSL_TRACED))	/* XXXSMP */
    150   1.1      manu 		return EPERM;
    151   1.1      manu 
    152   1.1      manu 	/*
    153   1.1      manu 	 *	(2) it's being traced by procfs (which has
    154   1.1      manu 	 *		 different signal delivery semantics),
    155   1.1      manu 	 */
    156  1.14        ad 	if (ISSET(t->p_slflag, PSL_FSTRACE))
    157   1.1      manu 		return EBUSY;
    158   1.1      manu 
    159   1.1      manu 	/*
    160   1.1      manu 	 *	(3) it's not being traced by _you_, or
    161   1.1      manu 	 */
    162   1.1      manu 	if (t->p_pptr != p)
    163   1.1      manu 		return EBUSY;
    164   1.1      manu 
    165   1.1      manu 	/*
    166   1.1      manu 	 *	(4) it's not currently stopped.
    167   1.1      manu 	 */
    168  1.14        ad 	if (t->p_stat != SSTOP || !t->p_waited)
    169   1.1      manu 		return EBUSY;
    170   1.1      manu 
    171  1.10   thorpej 	lt = LIST_FIRST(&t->p_lwps);
    172   1.1      manu 	*retval = 0;
    173   1.1      manu 
    174   1.1      manu 	switch (request) {
    175   1.1      manu 	case  LINUX_PTRACE_GETREGS:
    176   1.1      manu 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
    177   1.3  christos 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
    178   1.1      manu 		    M_TEMP, M_WAITOK);
    179   1.1      manu 
    180  1.10   thorpej 		error = process_read_regs(lt, regs);
    181   1.1      manu 		if (error != 0)
    182   1.1      manu 			goto out;
    183   1.1      manu 
    184  1.11     perry 		for (i=0; i<=31; i++)
    185   1.1      manu 			linux_regs->lgpr[i] = regs->fixreg[i];
    186   1.1      manu 		linux_regs->lnip = regs->pc;
    187   1.1      manu 		linux_regs->lmsr = 0;
    188   1.3  christos 		/* XXX Is that right? */
    189   1.3  christos 		linux_regs->lorig_gpr3 = regs->fixreg[3];
    190   1.1      manu 		linux_regs->lctr = regs->ctr;
    191   1.1      manu 		linux_regs->llink = regs->lr;
    192   1.1      manu 		linux_regs->lxer = regs->xer;
    193   1.1      manu 		linux_regs->lccr = regs->cr;
    194  1.11     perry 		linux_regs->lmq = 0;
    195   1.1      manu 		linux_regs->ltrap = 0;
    196   1.1      manu 		linux_regs->ldar = 0;
    197   1.1      manu 		linux_regs->ldsisr = 0;
    198   1.1      manu 		linux_regs->lresult = 0;
    199   1.1      manu 
    200  1.15  christos 		error = copyout(linux_regs, (void *)SCARG(uap, data),
    201   1.1      manu 			 sizeof(struct linux_pt_regs));
    202   1.1      manu 		goto out;
    203   1.1      manu 
    204   1.1      manu 	case  LINUX_PTRACE_SETREGS:
    205   1.1      manu 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
    206   1.3  christos 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
    207   1.1      manu 		    M_TEMP, M_WAITOK);
    208   1.1      manu 
    209  1.15  christos 		error = copyin((void *)SCARG(uap, data), linux_regs,
    210   1.1      manu 			 sizeof(struct linux_pt_regs));
    211   1.1      manu 		if (error != 0)
    212   1.1      manu 			goto out;
    213   1.1      manu 
    214  1.11     perry 		for (i=0; i<=31; i++)
    215   1.1      manu 			regs->fixreg[i] = linux_regs->lgpr[i];
    216   1.1      manu 		regs->lr = linux_regs->llink;
    217   1.1      manu 		regs->cr = linux_regs->lccr;
    218   1.1      manu 		regs->xer = linux_regs->lxer;
    219   1.1      manu 		regs->ctr = linux_regs->lctr;
    220   1.2      manu 		regs->pc = linux_regs->lnip; /* XXX */
    221   1.1      manu 
    222  1.10   thorpej 		error = process_write_regs(lt, regs);
    223   1.1      manu 		goto out;
    224   1.1      manu 
    225   1.1      manu 	case  LINUX_PTRACE_GETFPREGS:
    226   1.1      manu 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
    227   1.1      manu 		    M_TEMP, M_WAITOK);
    228   1.1      manu 		MALLOC(linux_fpreg, double *,
    229   1.1      manu 		    32*sizeof(double), M_TEMP, M_WAITOK);
    230   1.1      manu 
    231  1.10   thorpej 		error = process_read_fpregs(lt, fpregs);
    232   1.1      manu 		if (error != 0)
    233   1.1      manu 			goto out;
    234   1.1      manu 
    235   1.1      manu 		/* zero the contents if NetBSD fpreg structure is smaller */
    236   1.1      manu 		if (sizeof(struct fpreg) < (32*sizeof(double)))
    237   1.1      manu 			memset(linux_fpreg, '\0', (32*sizeof(double)));
    238   1.1      manu 
    239   1.1      manu 		memcpy(linux_fpreg, fpregs,
    240   1.1      manu 			min(32*sizeof(double), sizeof(struct fpreg)));
    241  1.15  christos 		error = copyout(linux_fpreg, (void *)SCARG(uap, data),
    242   1.1      manu 			 32*sizeof(double));
    243   1.1      manu 		goto out;
    244   1.1      manu 
    245   1.1      manu 	case  LINUX_PTRACE_SETFPREGS:
    246   1.1      manu 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
    247   1.1      manu 		    M_TEMP, M_WAITOK);
    248   1.1      manu 		MALLOC(linux_fpreg, double *,
    249   1.1      manu 		    32*sizeof(double), M_TEMP, M_WAITOK);
    250  1.15  christos 		error = copyin((void *)SCARG(uap, data), linux_fpreg,
    251   1.1      manu 			 32*sizeof(double));
    252   1.1      manu 		if (error != 0)
    253   1.1      manu 			goto out;
    254   1.1      manu 
    255   1.1      manu 		memset(fpregs, '\0', sizeof(struct fpreg));
    256   1.1      manu 		memcpy(fpregs, linux_fpreg,
    257   1.1      manu 		    min(32*sizeof(double), sizeof(struct fpreg)));
    258   1.1      manu 
    259  1.10   thorpej 		error = process_write_fpregs(lt, fpregs);
    260   1.1      manu 		goto out;
    261   1.1      manu 
    262   1.1      manu 	case  LINUX_PTRACE_PEEKUSR:
    263   1.1      manu 		addr = SCARG(uap, addr);
    264   1.5      manu 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
    265  1.10   thorpej 		error = process_read_regs(lt, regs);
    266   1.5      manu 		if (error)
    267   1.5      manu 			goto out;
    268   1.1      manu 
    269  1.16        ad 		uvm_lwp_hold(lt);	/* need full process info */
    270   1.1      manu 		error = 0;
    271  1.11     perry 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
    272  1.11     perry 		    (addr > LUSR_OFF(lu_comm_end)))
    273   1.1      manu 		 	error = 1;
    274   1.5      manu 		else if (addr == LUSR_OFF(u_tsize))
    275   1.1      manu 			*retval = p->p_vmspace->vm_tsize;
    276   1.1      manu 		else if (addr == LUSR_OFF(u_dsize))
    277   1.1      manu 			*retval = p->p_vmspace->vm_dsize;
    278   1.1      manu 		else if (addr == LUSR_OFF(u_ssize))
    279   1.1      manu 			*retval = p->p_vmspace->vm_ssize;
    280   1.1      manu 		else if (addr == LUSR_OFF(start_code))
    281   1.1      manu 			*retval = (register_t) p->p_vmspace->vm_taddr;
    282   1.1      manu 		else if (addr == LUSR_OFF(start_stack))
    283   1.1      manu 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
    284  1.11     perry 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
    285   1.5      manu 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
    286   1.5      manu 			*retval = regs->fixreg[addr / sizeof (register_t)];
    287   1.5      manu 		else if (addr == LUSR_REG_OFF(lnip))
    288   1.5      manu 			*retval = regs->pc;
    289   1.5      manu 		else if (addr == LUSR_REG_OFF(lctr))
    290   1.5      manu 			*retval = regs->ctr;
    291   1.5      manu 		else if (addr == LUSR_REG_OFF(llink))
    292   1.5      manu 			*retval = regs->lr;
    293   1.5      manu 		else if (addr == LUSR_REG_OFF(lxer))
    294   1.5      manu 			*retval = regs->xer;
    295   1.5      manu 		else if (addr == LUSR_REG_OFF(lccr))
    296   1.5      manu 			*retval = regs->cr;
    297   1.5      manu 		else if (addr == LUSR_OFF(signal))
    298   1.5      manu 			error = 1;
    299   1.5      manu 		else if (addr == LUSR_OFF(signal))
    300   1.5      manu 			error = 1;
    301   1.5      manu 		else if (addr == LUSR_OFF(magic))
    302   1.5      manu 			error = 1;
    303   1.5      manu 		else if (addr == LUSR_OFF(u_comm))
    304   1.5      manu 			error = 1;
    305   1.5      manu 		else {
    306   1.5      manu #ifdef DEBUG_LINUX
    307   1.5      manu 			printf("linux_ptrace: unsupported address: %d\n", addr);
    308   1.5      manu #endif
    309   1.5      manu 			error = 1;
    310   1.5      manu 		}
    311   1.5      manu 
    312  1.16        ad 		uvm_lwp_rele(lt);
    313   1.5      manu 
    314   1.6      manu 		if (error)
    315   1.6      manu 			goto out;
    316   1.6      manu 
    317  1.11     perry 		error = copyout (retval,
    318  1.15  christos 		    (void *)SCARG(uap, data), sizeof retval);
    319   1.5      manu 		*retval = SCARG(uap, data);
    320   1.6      manu 
    321   1.5      manu 		goto out;
    322   1.5      manu 
    323   1.5      manu 		break;
    324   1.5      manu 
    325   1.5      manu 	case  LINUX_PTRACE_POKEUSR: /* XXX Not tested */
    326   1.5      manu 		addr = SCARG(uap, addr);
    327   1.5      manu 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
    328  1.10   thorpej 		error = process_read_regs(lt, regs);
    329   1.5      manu 		if (error)
    330   1.5      manu 			goto out;
    331   1.5      manu 
    332  1.16        ad 		uvm_lwp_hold(lt);       /* need full process info */
    333   1.5      manu 		error = 0;
    334  1.11     perry 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
    335   1.5      manu 		    (addr > LUSR_OFF(lu_comm_end)))
    336   1.5      manu 		 	error = 1;
    337   1.5      manu 		else if (addr == LUSR_OFF(u_tsize))
    338   1.5      manu 			error = 1;
    339   1.5      manu 		else if (addr == LUSR_OFF(u_dsize))
    340   1.5      manu 			error = 1;
    341   1.5      manu 		else if (addr == LUSR_OFF(u_ssize))
    342   1.1      manu 			error = 1;
    343   1.5      manu 		else if (addr == LUSR_OFF(start_code))
    344   1.5      manu 			error = 1;
    345   1.5      manu 		else if (addr == LUSR_OFF(start_stack))
    346   1.1      manu 			error = 1;
    347  1.11     perry 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
    348   1.5      manu 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
    349  1.11     perry 			regs->fixreg[addr / sizeof (register_t)] =
    350   1.5      manu 			    (register_t)SCARG(uap, data);
    351   1.5      manu 		else if (addr == LUSR_REG_OFF(lnip))
    352   1.5      manu 			regs->pc = (register_t)SCARG(uap, data);
    353   1.5      manu 		else if (addr == LUSR_REG_OFF(lctr))
    354   1.5      manu 			regs->ctr = (register_t)SCARG(uap, data);
    355   1.5      manu 		else if (addr == LUSR_REG_OFF(llink))
    356   1.5      manu 			regs->lr = (register_t)SCARG(uap, data);
    357   1.5      manu 		else if (addr == LUSR_REG_OFF(lxer))
    358   1.5      manu 			regs->xer = (register_t)SCARG(uap, data);
    359   1.5      manu 		else if (addr == LUSR_REG_OFF(lccr))
    360   1.5      manu 			regs->cr = (register_t)SCARG(uap, data);
    361   1.5      manu 		else if (addr == LUSR_OFF(signal))
    362   1.1      manu 			error = 1;
    363   1.5      manu 		else if (addr == LUSR_OFF(magic))
    364   1.1      manu 			error = 1;
    365   1.5      manu 		else if (addr == LUSR_OFF(u_comm))
    366   1.5      manu 			error = 1;
    367   1.5      manu 		else {
    368   1.1      manu #ifdef DEBUG_LINUX
    369   1.1      manu 			printf("linux_ptrace: unsupported address: %d\n", addr);
    370   1.1      manu #endif
    371   1.1      manu 			error = 1;
    372   1.1      manu 		}
    373   1.1      manu 
    374  1.16        ad 		uvm_lwp_rele(lt);
    375   1.1      manu 
    376  1.10   thorpej 		error = process_write_regs(lt,regs);
    377  1.11     perry 		if (error)
    378   1.5      manu 			goto out;
    379   1.1      manu 
    380   1.1      manu 		break;
    381   1.1      manu 	default:
    382   1.1      manu 		/* never reached */
    383   1.1      manu 		break;
    384   1.1      manu 	}
    385   1.1      manu 	return EIO;
    386   1.1      manu 
    387   1.1      manu 	 out:
    388   1.1      manu 	if (regs)
    389   1.1      manu 		FREE(regs, M_TEMP);
    390   1.1      manu 	if (fpregs)
    391   1.1      manu 		FREE(fpregs, M_TEMP);
    392   1.1      manu 	if (linux_regs)
    393   1.1      manu 		FREE(linux_regs, M_TEMP);
    394   1.1      manu 	if (linux_fpreg)
    395   1.1      manu 		FREE(linux_fpreg, M_TEMP);
    396   1.1      manu 	return (error);
    397   1.1      manu }
    398