Home | History | Annotate | Line # | Download | only in powerpc
linux_ptrace.c revision 1.33
      1 /*	$NetBSD: linux_ptrace.c,v 1.33 2021/09/07 11:43:04 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999, 2001 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 and Emmanuel Dreyfus.
      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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.33 2021/09/07 11:43:04 riastradh Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/mount.h>
     37 #include <sys/proc.h>
     38 #include <sys/ptrace.h>
     39 #include <sys/systm.h>
     40 #include <sys/syscallargs.h>
     41 #include <uvm/uvm_extern.h>
     42 
     43 #include <machine/reg.h>
     44 
     45 #include <compat/linux/common/linux_types.h>
     46 #include <compat/linux/common/linux_ptrace.h>
     47 #include <compat/linux/common/linux_signal.h>
     48 
     49 #include <compat/linux/common/linux_util.h>
     50 #include <compat/linux/common/linux_machdep.h>
     51 #include <compat/linux/common/linux_emuldata.h>
     52 #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
     53 
     54 #include <compat/linux/linux_syscallargs.h>
     55 
     56 #include <lib/libkern/libkern.h>	/* for offsetof() */
     57 
     58 /*
     59  * From Linux's include/asm-ppc/ptrace.h.
     60  * structure used for storing reg context:  defined in linux_machdep.h
     61  * structure used for storing floating point context:
     62  * Linux just uses a double fpr[32], no struct
     63  */
     64 
     65 /*
     66  * user struct for linux process - this is used for Linux ptrace emulation
     67  * most of it is junk only used by gdb
     68  *
     69  * From Linux's include/asm-ppc/user.h
     70  *
     71  * XXX u_ar0 was a struct reg in Linux/powerpc
     72  * Can't find out what a struct regs is for Linux/powerpc,
     73  * so we use a struct pt_regs instead. don't know if this is right.
     74  */
     75 
     76 struct linux_user {
     77 	struct linux_pt_regs regs;
     78 #define lusr_startgdb regs
     79 	size_t u_tsize;
     80 	size_t u_dsize;
     81 	size_t u_ssize;
     82 	unsigned long start_code;
     83 	unsigned long start_data;
     84 	unsigned long start_stack;
     85 	long int signal;
     86 	struct linux_pt_regs *u_ar0;		/* help gdb find registers */
     87 	unsigned long magic;
     88 	char u_comm[32];
     89 #define lu_comm_end u_comm[31]
     90 };
     91 
     92 #define LUSR_OFF(member) offsetof(struct linux_user, member)
     93 #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member)
     94 #define ISSET(t, f) ((t) & (f))
     95 
     96 int linux_ptrace_disabled = 1;	/* bitrotted */
     97 
     98 /* XXX Check me! (From NetBSD/i386) */
     99 int
    100 linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap,
    101     register_t *retval)
    102 {
    103 	/* {
    104 		syscallarg(int) request;
    105 		syscallarg(int) pid;
    106 		syscallarg(int) addr;
    107 		syscallarg(int) data;
    108 	} */
    109 	struct proc *p = l->l_proc, *t;
    110 	struct lwp *lt;
    111 	struct reg *regs = NULL;
    112 	struct fpreg *fpregs = NULL;
    113 	struct linux_pt_regs *linux_regs = NULL;
    114 	double *linux_fpreg = NULL;  /* it's an array, not a struct */
    115 	int request, error, addr, i;
    116 
    117 	if (linux_ptrace_disabled)
    118 		return ENOSYS;
    119 
    120 	error = 0;
    121 	request = SCARG(uap, request);
    122 
    123 	switch (request) {
    124 	case LINUX_PTRACE_PEEKUSR:
    125 	case LINUX_PTRACE_POKEUSR:
    126 		regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
    127 		break;
    128 	case LINUX_PTRACE_GETREGS:
    129 	case LINUX_PTRACE_SETREGS:
    130 		regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
    131 		linux_regs = kmem_alloc(sizeof(*linux_regs), KM_SLEEP);
    132 		if (request == LINUX_PTRACE_SETREGS) {
    133 			error = copyin((void *)SCARG(uap, data), linux_regs,
    134 			    sizeof(struct linux_pt_regs));
    135 			if (error) {
    136 				goto out;
    137 			}
    138 		}
    139 		break;
    140 	case LINUX_PTRACE_GETFPREGS:
    141 	case LINUX_PTRACE_SETFPREGS:
    142 		fpregs = kmem_alloc(sizeof(struct fpreg), KM_SLEEP);
    143 		linux_fpreg = kmem_alloc(32 * sizeof(double), KM_SLEEP);
    144 		if (request == LINUX_PTRACE_SETFPREGS) {
    145 			error = copyin((void *)SCARG(uap, data), linux_fpreg,
    146 			    32 * sizeof(double));
    147 			if (error) {
    148 				goto out;
    149 			}
    150 		}
    151 		break;
    152 	default:
    153 		error = EIO;
    154 		goto out;
    155 	}
    156 
    157 	/* Find the process we are supposed to be operating on. */
    158 	mutex_enter(&proc_lock);
    159 	if ((t = proc_find(SCARG(uap, pid))) == NULL) {
    160 		mutex_exit(&proc_lock);
    161 		error = ESRCH;
    162 		goto out;
    163 	}
    164 	mutex_enter(t->p_lock);
    165 
    166 	/*
    167 	 * You cannot do what you want to the process if:
    168 	 * 1. It is not being traced at all,
    169 	 */
    170 	if (!ISSET(t->p_slflag, PSL_TRACED)) {
    171 		mutex_exit(t->p_lock);
    172 		mutex_exit(&proc_lock);
    173 		error = EPERM;
    174 		goto out;
    175 	}
    176 	/*
    177 	 * 2. It is not being traced by _you_, or
    178 	 * 3. It is not currently stopped.
    179 	 */
    180 	if (t->p_pptr != p || t->p_stat != SSTOP || !t->p_waited) {
    181 		mutex_exit(t->p_lock);
    182 		mutex_exit(&proc_lock);
    183 		error = EBUSY;
    184 		goto out;
    185 	}
    186 	mutex_exit(&proc_lock);
    187 	/* XXX: ptrace needs revamp for multi-threading support. */
    188 	if (t->p_nlwps > 1) {
    189 		mutex_exit(t->p_lock);
    190 		error = ENOSYS;
    191 		goto out;
    192 	}
    193 	lt = LIST_FIRST(&t->p_lwps);
    194 	*retval = 0;
    195 
    196 	switch (request) {
    197 	case LINUX_PTRACE_GETREGS:
    198 		error = process_read_regs(lt, regs);
    199 		mutex_exit(t->p_lock);
    200 		if (error) {
    201 			break;
    202 		}
    203 		memset(linux_regs, 0, sizeof(*linux_regs));
    204 		for (i = 0; i <= 31; i++) {
    205 			linux_regs->lgpr[i] = regs->fixreg[i];
    206 		}
    207 		linux_regs->lnip = regs->pc;
    208 		linux_regs->lmsr = 0;
    209 		/* XXX Is that right? */
    210 		linux_regs->lorig_gpr3 = regs->fixreg[3];
    211 		linux_regs->lctr = regs->ctr;
    212 		linux_regs->llink = regs->lr;
    213 		linux_regs->lxer = regs->xer;
    214 		linux_regs->lccr = regs->cr;
    215 		linux_regs->lmq = 0;
    216 		linux_regs->ltrap = 0;
    217 		linux_regs->ldar = 0;
    218 		linux_regs->ldsisr = 0;
    219 		linux_regs->lresult = 0;
    220 
    221 		error = copyout(linux_regs, (void *)SCARG(uap, data),
    222 		    sizeof(struct linux_pt_regs));
    223 		break;
    224 
    225 	case LINUX_PTRACE_SETREGS:
    226 		for (i = 0; i <= 31; i++) {
    227 			regs->fixreg[i] = linux_regs->lgpr[i];
    228 		}
    229 		regs->lr = linux_regs->llink;
    230 		regs->cr = linux_regs->lccr;
    231 		regs->xer = linux_regs->lxer;
    232 		regs->ctr = linux_regs->lctr;
    233 		regs->pc = linux_regs->lnip; /* XXX */
    234 
    235 		error = process_write_regs(lt, regs);
    236 		mutex_exit(t->p_lock);
    237 		break;
    238 
    239 	case LINUX_PTRACE_GETFPREGS:
    240 		error = process_read_fpregs(lt, fpregs, NULL);
    241 		mutex_exit(t->p_lock);
    242 		if (error) {
    243 			break;
    244 		}
    245 		/* Zero the contents if NetBSD fpreg structure is smaller */
    246 		if (sizeof(struct fpreg) < (32 * sizeof(double))) {
    247 			memset(linux_fpreg, '\0', (32 * sizeof(double)));
    248 		}
    249 		memcpy(linux_fpreg, fpregs,
    250 		    uimin(32 * sizeof(double), sizeof(struct fpreg)));
    251 		error = copyout(linux_fpreg, (void *)SCARG(uap, data),
    252 		    32 * sizeof(double));
    253 		break;
    254 
    255 	case LINUX_PTRACE_SETFPREGS:
    256 		memset(fpregs, '\0', sizeof(struct fpreg));
    257 		memcpy(fpregs, linux_fpreg,
    258 		    uimin(32 * sizeof(double), sizeof(struct fpreg)));
    259 		error = process_write_fpregs(lt, fpregs, sizeof fpregs);
    260 		mutex_exit(t->p_lock);
    261 		break;
    262 
    263 	case LINUX_PTRACE_PEEKUSR:
    264 		addr = SCARG(uap, addr);
    265 		error = process_read_regs(lt, regs);
    266 		mutex_exit(t->p_lock);
    267 		if (error) {
    268 			break;
    269 		}
    270 		error = 0;
    271 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
    272 		    (addr > LUSR_OFF(lu_comm_end)))
    273 		 	error = 1;
    274 		else if (addr == LUSR_OFF(u_tsize))
    275 			*retval = p->p_vmspace->vm_tsize;
    276 		else if (addr == LUSR_OFF(u_dsize))
    277 			*retval = p->p_vmspace->vm_dsize;
    278 		else if (addr == LUSR_OFF(u_ssize))
    279 			*retval = p->p_vmspace->vm_ssize;
    280 		else if (addr == LUSR_OFF(start_code))
    281 			*retval = (register_t) p->p_vmspace->vm_taddr;
    282 		else if (addr == LUSR_OFF(start_stack))
    283 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
    284 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
    285 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
    286 			*retval = regs->fixreg[addr / sizeof (register_t)];
    287 		else if (addr == LUSR_REG_OFF(lnip))
    288 			*retval = regs->pc;
    289 		else if (addr == LUSR_REG_OFF(lctr))
    290 			*retval = regs->ctr;
    291 		else if (addr == LUSR_REG_OFF(llink))
    292 			*retval = regs->lr;
    293 		else if (addr == LUSR_REG_OFF(lxer))
    294 			*retval = regs->xer;
    295 		else if (addr == LUSR_REG_OFF(lccr))
    296 			*retval = regs->cr;
    297 		else if (addr == LUSR_OFF(signal))
    298 			error = 1;
    299 		else if (addr == LUSR_OFF(magic))
    300 			error = 1;
    301 		else if (addr == LUSR_OFF(u_comm))
    302 			error = 1;
    303 		else {
    304 #ifdef DEBUG_LINUX
    305 			printf("linux_ptrace: unsupported address: %d\n", addr);
    306 #endif
    307 			error = 1;
    308 		}
    309 		if (error) {
    310 			break;
    311 		}
    312 		error = copyout (retval, (void *)SCARG(uap, data),
    313 		    sizeof(*retval));
    314 		*retval = SCARG(uap, data);
    315 		break;
    316 
    317 	case  LINUX_PTRACE_POKEUSR: /* XXX Not tested */
    318 		addr = SCARG(uap, addr);
    319 		error = process_read_regs(lt, regs);
    320 		if (error) {
    321 			mutex_exit(t->p_lock);
    322 			break;
    323 		}
    324 		error = 0;
    325 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
    326 		    (addr > LUSR_OFF(lu_comm_end)))
    327 		 	error = 1;
    328 		else if (addr == LUSR_OFF(u_tsize))
    329 			error = 1;
    330 		else if (addr == LUSR_OFF(u_dsize))
    331 			error = 1;
    332 		else if (addr == LUSR_OFF(u_ssize))
    333 			error = 1;
    334 		else if (addr == LUSR_OFF(start_code))
    335 			error = 1;
    336 		else if (addr == LUSR_OFF(start_stack))
    337 			error = 1;
    338 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
    339 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
    340 			regs->fixreg[addr / sizeof (register_t)] =
    341 			    (register_t)SCARG(uap, data);
    342 		else if (addr == LUSR_REG_OFF(lnip))
    343 			regs->pc = (register_t)SCARG(uap, data);
    344 		else if (addr == LUSR_REG_OFF(lctr))
    345 			regs->ctr = (register_t)SCARG(uap, data);
    346 		else if (addr == LUSR_REG_OFF(llink))
    347 			regs->lr = (register_t)SCARG(uap, data);
    348 		else if (addr == LUSR_REG_OFF(lxer))
    349 			regs->xer = (register_t)SCARG(uap, data);
    350 		else if (addr == LUSR_REG_OFF(lccr))
    351 			regs->cr = (register_t)SCARG(uap, data);
    352 		else if (addr == LUSR_OFF(signal))
    353 			error = 1;
    354 		else if (addr == LUSR_OFF(magic))
    355 			error = 1;
    356 		else if (addr == LUSR_OFF(u_comm))
    357 			error = 1;
    358 		else {
    359 #ifdef DEBUG_LINUX
    360 			printf("linux_ptrace: unsupported address: %d\n", addr);
    361 #endif
    362 			error = 1;
    363 		}
    364 		error = process_write_regs(lt,regs);
    365 		mutex_exit(t->p_lock);
    366 		break;
    367 	default:
    368 		mutex_exit(t->p_lock);
    369 		break;
    370 	}
    371 out:
    372 	if (regs)
    373 		kmem_free(regs, sizeof(*regs));
    374 	if (fpregs)
    375 		kmem_free(fpregs, sizeof(*fpregs));
    376 	if (linux_regs)
    377 		kmem_free(linux_regs, sizeof(*linux_regs));
    378 	if (linux_fpreg)
    379 		kmem_free(linux_fpreg, sizeof(*linux_fpreg));
    380 	return error;
    381 }
    382