Home | History | Annotate | Line # | Download | only in powerpc
linux_ptrace.c revision 1.26
      1  1.26  christos /*	$NetBSD: linux_ptrace.c,v 1.26 2014/09/21 16:59:08 christos 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  *
     19   1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      manu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      manu  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      manu  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      manu  */
     31   1.8     lukem 
     32   1.8     lukem #include <sys/cdefs.h>
     33  1.26  christos __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.26 2014/09/21 16:59:08 christos Exp $");
     34   1.1      manu 
     35   1.1      manu #include <sys/param.h>
     36   1.1      manu #include <sys/malloc.h>
     37   1.1      manu #include <sys/mount.h>
     38   1.1      manu #include <sys/proc.h>
     39   1.1      manu #include <sys/ptrace.h>
     40   1.1      manu #include <sys/systm.h>
     41   1.1      manu #include <sys/syscallargs.h>
     42   1.1      manu #include <uvm/uvm_extern.h>
     43   1.1      manu 
     44   1.1      manu #include <machine/reg.h>
     45   1.1      manu 
     46   1.1      manu #include <compat/linux/common/linux_types.h>
     47   1.1      manu #include <compat/linux/common/linux_ptrace.h>
     48   1.1      manu #include <compat/linux/common/linux_signal.h>
     49   1.1      manu 
     50   1.1      manu #include <compat/linux/common/linux_util.h>
     51   1.1      manu #include <compat/linux/common/linux_machdep.h>
     52   1.1      manu #include <compat/linux/common/linux_emuldata.h>
     53   1.1      manu #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
     54   1.1      manu 
     55   1.1      manu #include <compat/linux/linux_syscallargs.h>
     56   1.1      manu 
     57   1.1      manu #include <lib/libkern/libkern.h>	/* for offsetof() */
     58   1.1      manu 
     59  1.11     perry /*
     60  1.11     perry  * From Linux's include/asm-ppc/ptrace.h.
     61  1.11     perry  * structure used for storing reg context:  defined in linux_machdep.h
     62   1.1      manu  * structure used for storing floating point context:
     63  1.11     perry  * Linux just uses a double fpr[32], no struct
     64   1.1      manu  */
     65  1.11     perry 
     66  1.11     perry /*
     67  1.11     perry  * user struct for linux process - this is used for Linux ptrace emulation
     68  1.11     perry  * most of it is junk only used by gdb
     69  1.11     perry  *
     70  1.11     perry  * From Linux's include/asm-ppc/user.h
     71  1.11     perry  *
     72  1.11     perry  * XXX u_ar0 was a struct reg in Linux/powerpc
     73  1.11     perry  * Can't find out what a struct regs is for Linux/powerpc,
     74   1.1      manu  * so we use a struct pt_regs instead. don't know if this is right.
     75   1.1      manu  */
     76   1.1      manu 
     77   1.1      manu struct linux_user {
     78  1.11     perry 	struct linux_pt_regs regs;
     79   1.4      manu #define lusr_startgdb regs
     80  1.11     perry 	size_t u_tsize;
     81  1.11     perry 	size_t u_dsize;
     82  1.11     perry 	size_t u_ssize;
     83   1.1      manu 	unsigned long start_code;
     84   1.1      manu 	unsigned long start_data;
     85   1.1      manu 	unsigned long start_stack;
     86  1.11     perry 	long int signal;
     87   1.3  christos 	struct linux_pt_regs *u_ar0;		/* help gdb find registers */
     88  1.11     perry 	unsigned long magic;
     89  1.11     perry 	char u_comm[32];
     90   1.4      manu #define lu_comm_end u_comm[31]
     91   1.1      manu };
     92   1.1      manu 
     93   1.1      manu #define LUSR_OFF(member) offsetof(struct linux_user, member)
     94   1.5      manu #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member)
     95   1.1      manu #define ISSET(t, f) ((t) & (f))
     96   1.1      manu 
     97  1.18        ad int linux_ptrace_disabled = 1;	/* bitrotted */
     98  1.18        ad 
     99  1.17       dsl /* XXX Check me! (From NetBSD/i386) */
    100   1.1      manu int
    101  1.23     rmind linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap,
    102  1.23     rmind     register_t *retval)
    103   1.1      manu {
    104  1.17       dsl 	/* {
    105   1.1      manu 		syscallarg(int) request;
    106   1.1      manu 		syscallarg(int) pid;
    107   1.1      manu 		syscallarg(int) addr;
    108   1.1      manu 		syscallarg(int) data;
    109  1.17       dsl 	} */
    110  1.23     rmind 	struct proc *p = l->l_proc, *t;
    111  1.10   thorpej 	struct lwp *lt;
    112   1.1      manu 	struct reg *regs = NULL;
    113   1.1      manu 	struct fpreg *fpregs = NULL;
    114   1.1      manu 	struct linux_pt_regs *linux_regs = NULL;
    115   1.3  christos 	double *linux_fpreg = NULL;  /* it's an array, not a struct */
    116  1.23     rmind 	int request, error, addr, i;
    117   1.1      manu 
    118  1.18        ad 	if (linux_ptrace_disabled)
    119  1.18        ad 		return ENOSYS;
    120  1.18        ad 
    121  1.23     rmind 	error = 0;
    122  1.23     rmind 	request = SCARG(uap, request);
    123  1.23     rmind 
    124  1.23     rmind 	switch (request) {
    125  1.23     rmind 	case LINUX_PTRACE_PEEKUSR:
    126  1.23     rmind 	case LINUX_PTRACE_POKEUSR:
    127  1.23     rmind 		regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
    128  1.23     rmind 		break;
    129  1.23     rmind 	case LINUX_PTRACE_GETREGS:
    130  1.23     rmind 	case LINUX_PTRACE_SETREGS:
    131  1.23     rmind 		regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
    132  1.23     rmind 		linux_regs = kmem_alloc(sizeof(*linux_regs), KM_SLEEP);
    133  1.23     rmind 		if (request == LINUX_PTRACE_SETREGS) {
    134  1.23     rmind 			error = copyin((void *)SCARG(uap, data), linux_regs,
    135  1.23     rmind 			    sizeof(struct linux_pt_regs));
    136  1.23     rmind 			if (error) {
    137  1.23     rmind 				goto out;
    138  1.23     rmind 			}
    139  1.23     rmind 		}
    140  1.23     rmind 		break;
    141  1.23     rmind 	case LINUX_PTRACE_GETFPREGS:
    142  1.23     rmind 	case LINUX_PTRACE_SETFPREGS:
    143  1.23     rmind 		fpregs = kmem_alloc(sizeof(struct fpreg), KM_SLEEP);
    144  1.23     rmind 		linux_fpreg = kmem_alloc(32 * sizeof(double), KM_SLEEP);
    145  1.23     rmind 		if (request == LINUX_PTRACE_SETFPREGS) {
    146  1.23     rmind 			error = copyin((void *)SCARG(uap, data), linux_fpreg,
    147  1.23     rmind 			    32 * sizeof(double));
    148  1.23     rmind 			if (error) {
    149  1.23     rmind 				goto out;
    150  1.23     rmind 			}
    151  1.23     rmind 		}
    152  1.23     rmind 		break;
    153  1.23     rmind 	default:
    154  1.23     rmind 		error = EIO;
    155  1.23     rmind 		goto out;
    156   1.3  christos 	}
    157   1.1      manu 
    158  1.23     rmind 	/* Find the process we are supposed to be operating on. */
    159  1.23     rmind 	mutex_enter(proc_lock);
    160  1.23     rmind 	if ((t = proc_find(SCARG(uap, pid))) == NULL) {
    161  1.23     rmind 		mutex_exit(proc_lock);
    162  1.26  christos 		goto out;
    163  1.23     rmind 	}
    164  1.23     rmind 	mutex_enter(t->p_lock);
    165  1.23     rmind 	mutex_exit(proc_lock);
    166   1.1      manu 
    167   1.1      manu 	/*
    168  1.23     rmind 	 * You cannot do what you want to the process if:
    169  1.23     rmind 	 * 1. It is not being traced at all,
    170   1.1      manu 	 */
    171  1.23     rmind 	if (!ISSET(t->p_slflag, PSL_TRACED)) {
    172  1.23     rmind 		mutex_exit(t->p_lock);
    173  1.23     rmind 		error = EPERM;
    174  1.23     rmind 		goto out;
    175  1.23     rmind 	}
    176   1.1      manu 	/*
    177  1.23     rmind 	 * 2. It is being traced by procfs (which has different signal
    178  1.23     rmind 	 *    delivery semantics),
    179  1.23     rmind 	 * 3. It is not being traced by _you_, or
    180  1.23     rmind 	 * 4. It is not currently stopped.
    181   1.1      manu 	 */
    182  1.23     rmind 	if (ISSET(t->p_slflag, PSL_FSTRACE) || t->p_pptr != p ||
    183  1.23     rmind 	    t->p_stat != SSTOP || !t->p_waited) {
    184  1.23     rmind 		mutex_exit(t->p_lock);
    185  1.23     rmind 		error = EBUSY;
    186  1.23     rmind 		goto out;
    187  1.23     rmind 	}
    188  1.23     rmind 	/* XXX: ptrace needs revamp for multi-threading support. */
    189  1.23     rmind 	if (t->p_nlwps > 1) {
    190  1.23     rmind 		mutex_exit(t->p_lock);
    191  1.23     rmind 		error = ENOSYS;
    192  1.23     rmind 		goto out;
    193  1.23     rmind 	}
    194  1.10   thorpej 	lt = LIST_FIRST(&t->p_lwps);
    195   1.1      manu 	*retval = 0;
    196   1.1      manu 
    197   1.1      manu 	switch (request) {
    198  1.23     rmind 	case LINUX_PTRACE_GETREGS:
    199  1.10   thorpej 		error = process_read_regs(lt, regs);
    200  1.23     rmind 		mutex_exit(t->p_lock);
    201  1.23     rmind 		if (error) {
    202  1.23     rmind 			break;
    203  1.23     rmind 		}
    204  1.23     rmind 		for (i = 0; i <= 31; i++) {
    205   1.1      manu 			linux_regs->lgpr[i] = regs->fixreg[i];
    206  1.23     rmind 		}
    207   1.1      manu 		linux_regs->lnip = regs->pc;
    208   1.1      manu 		linux_regs->lmsr = 0;
    209   1.3  christos 		/* XXX Is that right? */
    210   1.3  christos 		linux_regs->lorig_gpr3 = regs->fixreg[3];
    211   1.1      manu 		linux_regs->lctr = regs->ctr;
    212   1.1      manu 		linux_regs->llink = regs->lr;
    213   1.1      manu 		linux_regs->lxer = regs->xer;
    214   1.1      manu 		linux_regs->lccr = regs->cr;
    215  1.11     perry 		linux_regs->lmq = 0;
    216   1.1      manu 		linux_regs->ltrap = 0;
    217   1.1      manu 		linux_regs->ldar = 0;
    218   1.1      manu 		linux_regs->ldsisr = 0;
    219   1.1      manu 		linux_regs->lresult = 0;
    220   1.1      manu 
    221  1.15  christos 		error = copyout(linux_regs, (void *)SCARG(uap, data),
    222  1.23     rmind 		    sizeof(struct linux_pt_regs));
    223  1.23     rmind 		break;
    224   1.1      manu 
    225  1.23     rmind 	case LINUX_PTRACE_SETREGS:
    226  1.23     rmind 		for (i = 0; i <= 31; i++) {
    227   1.1      manu 			regs->fixreg[i] = linux_regs->lgpr[i];
    228  1.23     rmind 		}
    229   1.1      manu 		regs->lr = linux_regs->llink;
    230   1.1      manu 		regs->cr = linux_regs->lccr;
    231   1.1      manu 		regs->xer = linux_regs->lxer;
    232   1.1      manu 		regs->ctr = linux_regs->lctr;
    233   1.2      manu 		regs->pc = linux_regs->lnip; /* XXX */
    234   1.1      manu 
    235  1.10   thorpej 		error = process_write_regs(lt, regs);
    236  1.23     rmind 		mutex_exit(t->p_lock);
    237  1.23     rmind 		break;
    238   1.1      manu 
    239  1.23     rmind 	case LINUX_PTRACE_GETFPREGS:
    240  1.24       dsl 		error = process_read_fpregs(lt, fpregs, NULL);
    241  1.23     rmind 		mutex_exit(t->p_lock);
    242  1.23     rmind 		if (error) {
    243  1.23     rmind 			break;
    244  1.23     rmind 		}
    245  1.23     rmind 		/* Zero the contents if NetBSD fpreg structure is smaller */
    246  1.23     rmind 		if (sizeof(struct fpreg) < (32 * sizeof(double))) {
    247  1.23     rmind 			memset(linux_fpreg, '\0', (32 * sizeof(double)));
    248  1.23     rmind 		}
    249   1.1      manu 		memcpy(linux_fpreg, fpregs,
    250  1.23     rmind 		    min(32 * sizeof(double), sizeof(struct fpreg)));
    251  1.15  christos 		error = copyout(linux_fpreg, (void *)SCARG(uap, data),
    252  1.23     rmind 		    32 * sizeof(double));
    253  1.23     rmind 		break;
    254   1.1      manu 
    255  1.23     rmind 	case LINUX_PTRACE_SETFPREGS:
    256   1.1      manu 		memset(fpregs, '\0', sizeof(struct fpreg));
    257   1.1      manu 		memcpy(fpregs, linux_fpreg,
    258  1.23     rmind 		    min(32 * sizeof(double), sizeof(struct fpreg)));
    259  1.24       dsl 		error = process_write_fpregs(lt, fpregs, sizeof fpregs);
    260  1.23     rmind 		mutex_exit(t->p_lock);
    261  1.23     rmind 		break;
    262   1.1      manu 
    263  1.23     rmind 	case LINUX_PTRACE_PEEKUSR:
    264   1.1      manu 		addr = SCARG(uap, addr);
    265  1.10   thorpej 		error = process_read_regs(lt, regs);
    266  1.23     rmind 		mutex_exit(t->p_lock);
    267  1.23     rmind 		if (error) {
    268  1.23     rmind 			break;
    269  1.23     rmind 		}
    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(magic))
    300   1.5      manu 			error = 1;
    301   1.5      manu 		else if (addr == LUSR_OFF(u_comm))
    302   1.5      manu 			error = 1;
    303   1.5      manu 		else {
    304   1.5      manu #ifdef DEBUG_LINUX
    305   1.5      manu 			printf("linux_ptrace: unsupported address: %d\n", addr);
    306   1.5      manu #endif
    307   1.5      manu 			error = 1;
    308   1.5      manu 		}
    309  1.23     rmind 		if (error) {
    310  1.23     rmind 			break;
    311  1.23     rmind 		}
    312  1.23     rmind 		error = copyout (retval, (void *)SCARG(uap, data),
    313  1.23     rmind 		    sizeof(retval));
    314   1.5      manu 		*retval = SCARG(uap, data);
    315   1.5      manu 		break;
    316   1.5      manu 
    317   1.5      manu 	case  LINUX_PTRACE_POKEUSR: /* XXX Not tested */
    318   1.5      manu 		addr = SCARG(uap, addr);
    319  1.10   thorpej 		error = process_read_regs(lt, regs);
    320  1.23     rmind 		if (error) {
    321  1.23     rmind 			mutex_exit(t->p_lock);
    322  1.23     rmind 			break;
    323  1.23     rmind 		}
    324   1.5      manu 		error = 0;
    325  1.11     perry 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
    326   1.5      manu 		    (addr > LUSR_OFF(lu_comm_end)))
    327   1.5      manu 		 	error = 1;
    328   1.5      manu 		else if (addr == LUSR_OFF(u_tsize))
    329   1.5      manu 			error = 1;
    330   1.5      manu 		else if (addr == LUSR_OFF(u_dsize))
    331   1.5      manu 			error = 1;
    332   1.5      manu 		else if (addr == LUSR_OFF(u_ssize))
    333   1.1      manu 			error = 1;
    334   1.5      manu 		else if (addr == LUSR_OFF(start_code))
    335   1.5      manu 			error = 1;
    336   1.5      manu 		else if (addr == LUSR_OFF(start_stack))
    337   1.1      manu 			error = 1;
    338  1.11     perry 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
    339   1.5      manu 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
    340  1.11     perry 			regs->fixreg[addr / sizeof (register_t)] =
    341   1.5      manu 			    (register_t)SCARG(uap, data);
    342   1.5      manu 		else if (addr == LUSR_REG_OFF(lnip))
    343   1.5      manu 			regs->pc = (register_t)SCARG(uap, data);
    344   1.5      manu 		else if (addr == LUSR_REG_OFF(lctr))
    345   1.5      manu 			regs->ctr = (register_t)SCARG(uap, data);
    346   1.5      manu 		else if (addr == LUSR_REG_OFF(llink))
    347   1.5      manu 			regs->lr = (register_t)SCARG(uap, data);
    348   1.5      manu 		else if (addr == LUSR_REG_OFF(lxer))
    349   1.5      manu 			regs->xer = (register_t)SCARG(uap, data);
    350   1.5      manu 		else if (addr == LUSR_REG_OFF(lccr))
    351   1.5      manu 			regs->cr = (register_t)SCARG(uap, data);
    352   1.5      manu 		else if (addr == LUSR_OFF(signal))
    353   1.1      manu 			error = 1;
    354   1.5      manu 		else if (addr == LUSR_OFF(magic))
    355   1.1      manu 			error = 1;
    356   1.5      manu 		else if (addr == LUSR_OFF(u_comm))
    357   1.5      manu 			error = 1;
    358   1.5      manu 		else {
    359   1.1      manu #ifdef DEBUG_LINUX
    360   1.1      manu 			printf("linux_ptrace: unsupported address: %d\n", addr);
    361   1.1      manu #endif
    362   1.1      manu 			error = 1;
    363   1.1      manu 		}
    364  1.10   thorpej 		error = process_write_regs(lt,regs);
    365  1.23     rmind 		mutex_exit(t->p_lock);
    366   1.1      manu 		break;
    367   1.1      manu 	default:
    368  1.23     rmind 		mutex_exit(t->p_lock);
    369   1.1      manu 		break;
    370   1.1      manu 	}
    371  1.23     rmind out:
    372   1.1      manu 	if (regs)
    373  1.23     rmind 		kmem_free(regs, sizeof(*regs));
    374   1.1      manu 	if (fpregs)
    375  1.23     rmind 		kmem_free(fpregs, sizeof(*fpregs));
    376   1.1      manu 	if (linux_regs)
    377  1.23     rmind 		kmem_free(linux_regs, sizeof(*linux_regs));
    378   1.1      manu 	if (linux_fpreg)
    379  1.23     rmind 		kmem_free(linux_fpreg, sizeof(*linux_fpreg));
    380  1.23     rmind 	return error;
    381   1.1      manu }
    382