Home | History | Annotate | Line # | Download | only in kern
sys_ptrace_common.c revision 1.26
      1 /*	$NetBSD: sys_ptrace_common.c,v 1.26 2017/12/09 05:18:45 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      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  * Copyright (c) 1982, 1986, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  * (c) UNIX System Laboratories, Inc.
     36  * All or some portions of this file are derived from material licensed
     37  * to the University of California by American Telephone and Telegraph
     38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39  * the permission of UNIX System Laboratories, Inc.
     40  *
     41  * This code is derived from software contributed to Berkeley by
     42  * Jan-Simon Pendry.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
     69  */
     70 
     71 /*-
     72  * Copyright (c) 1993 Jan-Simon Pendry.
     73  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     74  *
     75  * This code is derived from software contributed to Berkeley by
     76  * Jan-Simon Pendry.
     77  *
     78  * Redistribution and use in source and binary forms, with or without
     79  * modification, are permitted provided that the following conditions
     80  * are met:
     81  * 1. Redistributions of source code must retain the above copyright
     82  *    notice, this list of conditions and the following disclaimer.
     83  * 2. Redistributions in binary form must reproduce the above copyright
     84  *    notice, this list of conditions and the following disclaimer in the
     85  *    documentation and/or other materials provided with the distribution.
     86  * 3. All advertising materials mentioning features or use of this software
     87  *    must display the following acknowledgement:
     88  *	This product includes software developed by the University of
     89  *	California, Berkeley and its contributors.
     90  * 4. Neither the name of the University nor the names of its contributors
     91  *    may be used to endorse or promote products derived from this software
     92  *    without specific prior written permission.
     93  *
     94  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  * SUCH DAMAGE.
    105  *
    106  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
    107  */
    108 
    109 /*
    110  * References:
    111  *	(1) Bach's "The Design of the UNIX Operating System",
    112  *	(2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
    113  *	(3) the "4.4BSD Programmer's Reference Manual" published
    114  *		by USENIX and O'Reilly & Associates.
    115  * The 4.4BSD PRM does a reasonably good job of documenting what the various
    116  * ptrace() requests should actually do, and its text is quoted several times
    117  * in this file.
    118  */
    119 
    120 #include <sys/cdefs.h>
    121 __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.26 2017/12/09 05:18:45 christos Exp $");
    122 
    123 #ifdef _KERNEL_OPT
    124 #include "opt_ptrace.h"
    125 #include "opt_ktrace.h"
    126 #include "opt_pax.h"
    127 #include "opt_compat_netbsd32.h"
    128 #endif
    129 
    130 #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
    131     && !defined(_RUMPKERNEL)
    132 #define COMPAT_NETBSD32
    133 #endif
    134 
    135 #include <sys/param.h>
    136 #include <sys/systm.h>
    137 #include <sys/proc.h>
    138 #include <sys/errno.h>
    139 #include <sys/exec.h>
    140 #include <sys/pax.h>
    141 #include <sys/ptrace.h>
    142 #include <sys/uio.h>
    143 #include <sys/ras.h>
    144 #include <sys/kmem.h>
    145 #include <sys/kauth.h>
    146 #include <sys/mount.h>
    147 #include <sys/syscallargs.h>
    148 #include <sys/module.h>
    149 #include <sys/condvar.h>
    150 #include <sys/mutex.h>
    151 
    152 #include <uvm/uvm_extern.h>
    153 
    154 #include <machine/reg.h>
    155 
    156 #ifdef PTRACE
    157 
    158 # ifdef DEBUG
    159 #  define DPRINTF(a) uprintf a
    160 # else
    161 #  define DPRINTF(a)
    162 # endif
    163 
    164 static kauth_listener_t ptrace_listener;
    165 static int process_auxv_offset(struct proc *, struct uio *);
    166 
    167 #if 0
    168 static int ptrace_cbref;
    169 static kmutex_t ptrace_mtx;
    170 static kcondvar_t ptrace_cv;
    171 #endif
    172 
    173 static int
    174 ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    175     void *arg0, void *arg1, void *arg2, void *arg3)
    176 {
    177 	struct proc *p;
    178 	int result;
    179 
    180 	result = KAUTH_RESULT_DEFER;
    181 	p = arg0;
    182 
    183 #if 0
    184 	mutex_enter(&ptrace_mtx);
    185 	ptrace_cbref++;
    186 	mutex_exit(&ptrace_mtx);
    187 #endif
    188 	if (action != KAUTH_PROCESS_PTRACE)
    189 		goto out;
    190 
    191 	switch ((u_long)arg1) {
    192 	case PT_TRACE_ME:
    193 	case PT_ATTACH:
    194 	case PT_WRITE_I:
    195 	case PT_WRITE_D:
    196 	case PT_READ_I:
    197 	case PT_READ_D:
    198 	case PT_IO:
    199 #ifdef PT_GETREGS
    200 	case PT_GETREGS:
    201 #endif
    202 #ifdef PT_SETREGS
    203 	case PT_SETREGS:
    204 #endif
    205 #ifdef PT_GETFPREGS
    206 	case PT_GETFPREGS:
    207 #endif
    208 #ifdef PT_SETFPREGS
    209 	case PT_SETFPREGS:
    210 #endif
    211 #ifdef PT_GETDBREGS
    212 	case PT_GETDBREGS:
    213 #endif
    214 #ifdef PT_SETDBREGS
    215 	case PT_SETDBREGS:
    216 #endif
    217 	case PT_SET_EVENT_MASK:
    218 	case PT_GET_EVENT_MASK:
    219 	case PT_GET_PROCESS_STATE:
    220 	case PT_SET_SIGINFO:
    221 	case PT_GET_SIGINFO:
    222 	case PT_SET_SIGMASK:
    223 	case PT_GET_SIGMASK:
    224 #ifdef __HAVE_PTRACE_MACHDEP
    225 	PTRACE_MACHDEP_REQUEST_CASES
    226 #endif
    227 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    228 		    ISSET(p->p_flag, PK_SUGID)) {
    229 			break;
    230 		}
    231 
    232 		result = KAUTH_RESULT_ALLOW;
    233 
    234 	break;
    235 
    236 #ifdef PT_STEP
    237 	case PT_STEP:
    238 	case PT_SETSTEP:
    239 	case PT_CLEARSTEP:
    240 #endif
    241 	case PT_CONTINUE:
    242 	case PT_KILL:
    243 	case PT_DETACH:
    244 	case PT_LWPINFO:
    245 	case PT_SYSCALL:
    246 	case PT_SYSCALLEMU:
    247 	case PT_DUMPCORE:
    248 	case PT_RESUME:
    249 	case PT_SUSPEND:
    250 		result = KAUTH_RESULT_ALLOW;
    251 		break;
    252 
    253 	default:
    254 		break;
    255 	}
    256 
    257  out:
    258 #if 0
    259 	mutex_enter(&ptrace_mtx);
    260 	if (--ptrace_cbref == 0)
    261 		cv_broadcast(&ptrace_cv);
    262 	mutex_exit(&ptrace_mtx);
    263 #endif
    264 
    265 	return result;
    266 }
    267 
    268 int
    269 ptrace_init(void)
    270 {
    271 
    272 #if 0
    273 	mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
    274 	cv_init(&ptrace_cv, "ptracecb");
    275 	ptrace_cbref = 0;
    276 #endif
    277 	ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    278 	    ptrace_listener_cb, NULL);
    279 	return 0;
    280 }
    281 
    282 int
    283 ptrace_fini(void)
    284 {
    285 
    286 	kauth_unlisten_scope(ptrace_listener);
    287 
    288 #if 0
    289 	/* Make sure no-one is executing our kauth listener */
    290 
    291 	mutex_enter(&ptrace_mtx);
    292 	while (ptrace_cbref != 0)
    293 		cv_wait(&ptrace_cv, &ptrace_mtx);
    294 	mutex_exit(&ptrace_mtx);
    295 	mutex_destroy(&ptrace_mtx);
    296 	cv_destroy(&ptrace_cv);
    297 #endif
    298 
    299 	return 0;
    300 }
    301 
    302 int
    303 do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
    304     void *addr, int data, register_t *retval)
    305 {
    306 	struct proc *p = l->l_proc;
    307 	struct lwp *lt;
    308 	struct lwp *lt2;
    309 	struct proc *t;				/* target process */
    310 	struct uio uio;
    311 	struct iovec iov;
    312 	struct ptrace_io_desc piod;
    313 	struct ptrace_event pe;
    314 	struct ptrace_state ps;
    315 	struct ptrace_lwpinfo pl;
    316 	struct ptrace_siginfo psi;
    317 	struct vmspace *vm;
    318 	int error, write, tmp, pheld;
    319 	int signo = 0;
    320 	int resume_all;
    321 	ksiginfo_t ksi;
    322 	char *path;
    323 	int len = 0;
    324 	error = 0;
    325 
    326 	/*
    327 	 * If attaching or detaching, we need to get a write hold on the
    328 	 * proclist lock so that we can re-parent the target process.
    329 	 */
    330 	mutex_enter(proc_lock);
    331 
    332 	/* "A foolish consistency..." XXX */
    333 	if (req == PT_TRACE_ME) {
    334 		t = p;
    335 		mutex_enter(t->p_lock);
    336 	} else {
    337 		/* Find the process we're supposed to be operating on. */
    338 		t = proc_find(pid);
    339 		if (t == NULL) {
    340 			mutex_exit(proc_lock);
    341 			return ESRCH;
    342 		}
    343 
    344 		/* XXX-elad */
    345 		mutex_enter(t->p_lock);
    346 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
    347 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    348 		if (error) {
    349 			mutex_exit(proc_lock);
    350 			mutex_exit(t->p_lock);
    351 			return ESRCH;
    352 		}
    353 	}
    354 
    355 	/*
    356 	 * Grab a reference on the process to prevent it from execing or
    357 	 * exiting.
    358 	 */
    359 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
    360 		mutex_exit(proc_lock);
    361 		mutex_exit(t->p_lock);
    362 		return EBUSY;
    363 	}
    364 
    365 	/* Make sure we can operate on it. */
    366 	switch (req) {
    367 	case  PT_TRACE_ME:
    368 		/* Saying that you're being traced is always legal. */
    369 		break;
    370 
    371 	case  PT_ATTACH:
    372 		/*
    373 		 * You can't attach to a process if:
    374 		 *	(1) it's the process that's doing the attaching,
    375 		 */
    376 		if (t->p_pid == p->p_pid) {
    377 			error = EINVAL;
    378 			break;
    379 		}
    380 
    381 		/*
    382 		 *  (2) it's a system process
    383 		 */
    384 		if (t->p_flag & PK_SYSTEM) {
    385 			error = EPERM;
    386 			break;
    387 		}
    388 
    389 		/*
    390 		 *	(3) it's already being traced, or
    391 		 */
    392 		if (ISSET(t->p_slflag, PSL_TRACED)) {
    393 			error = EBUSY;
    394 			break;
    395 		}
    396 
    397 		/*
    398 		 * 	(4) the tracer is chrooted, and its root directory is
    399 		 * 	    not at or above the root directory of the tracee
    400 		 */
    401 		mutex_exit(t->p_lock);	/* XXXSMP */
    402 		tmp = proc_isunder(t, l);
    403 		mutex_enter(t->p_lock);	/* XXXSMP */
    404 		if (!tmp) {
    405 			error = EPERM;
    406 			break;
    407 		}
    408 		break;
    409 
    410 	case  PT_READ_I:
    411 	case  PT_READ_D:
    412 	case  PT_WRITE_I:
    413 	case  PT_WRITE_D:
    414 	case  PT_IO:
    415 	case  PT_SET_SIGINFO:
    416 	case  PT_GET_SIGINFO:
    417 	case  PT_SET_SIGMASK:
    418 	case  PT_GET_SIGMASK:
    419 #ifdef PT_GETREGS
    420 	case  PT_GETREGS:
    421 #endif
    422 #ifdef PT_SETREGS
    423 	case  PT_SETREGS:
    424 #endif
    425 #ifdef PT_GETFPREGS
    426 	case  PT_GETFPREGS:
    427 #endif
    428 #ifdef PT_SETFPREGS
    429 	case  PT_SETFPREGS:
    430 #endif
    431 #ifdef PT_GETDBREGS
    432 	case  PT_GETDBREGS:
    433 #endif
    434 #ifdef PT_SETDBREGS
    435 	case  PT_SETDBREGS:
    436 #endif
    437 #ifdef __HAVE_PTRACE_MACHDEP
    438 	PTRACE_MACHDEP_REQUEST_CASES
    439 #endif
    440 		/*
    441 		 * You can't read/write the memory or registers of a process
    442 		 * if the tracer is chrooted, and its root directory is not at
    443 		 * or above the root directory of the tracee.
    444 		 */
    445 		mutex_exit(t->p_lock);	/* XXXSMP */
    446 		tmp = proc_isunder(t, l);
    447 		mutex_enter(t->p_lock);	/* XXXSMP */
    448 		if (!tmp) {
    449 			error = EPERM;
    450 			break;
    451 		}
    452 		/*FALLTHROUGH*/
    453 
    454 	case  PT_CONTINUE:
    455 	case  PT_KILL:
    456 	case  PT_DETACH:
    457 	case  PT_LWPINFO:
    458 	case  PT_SYSCALL:
    459 	case  PT_SYSCALLEMU:
    460 	case  PT_DUMPCORE:
    461 #ifdef PT_STEP
    462 	case  PT_STEP:
    463 	case  PT_SETSTEP:
    464 	case  PT_CLEARSTEP:
    465 #endif
    466 	case  PT_SET_EVENT_MASK:
    467 	case  PT_GET_EVENT_MASK:
    468 	case  PT_GET_PROCESS_STATE:
    469 	case  PT_RESUME:
    470 	case  PT_SUSPEND:
    471 		/*
    472 		 * You can't do what you want to the process if:
    473 		 *	(1) It's not being traced at all,
    474 		 */
    475 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
    476 			error = EPERM;
    477 			break;
    478 		}
    479 
    480 		/*
    481 		 *	(2) it's not being traced by _you_, or
    482 		 */
    483 		if (t->p_pptr != p) {
    484 			DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
    485 			    p->p_pid));
    486 			error = EBUSY;
    487 			break;
    488 		}
    489 
    490 		/*
    491 		 *	(3) it's not currently stopped.
    492 		 */
    493 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
    494 			DPRINTF(("stat %d flag %d\n", t->p_stat,
    495 			    !t->p_waited));
    496 			error = EBUSY;
    497 			break;
    498 		}
    499 		break;
    500 
    501 	default:			/* It was not a legal request. */
    502 		error = EINVAL;
    503 		break;
    504 	}
    505 
    506 	if (error == 0) {
    507 		error = kauth_authorize_process(l->l_cred,
    508 		    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
    509 		    NULL, NULL);
    510 	}
    511 	if (error == 0) {
    512 		lt = lwp_find_first(t);
    513 		if (lt == NULL)
    514 			error = ESRCH;
    515 	}
    516 
    517 	if (error != 0) {
    518 		mutex_exit(proc_lock);
    519 		mutex_exit(t->p_lock);
    520 		rw_exit(&t->p_reflock);
    521 		return error;
    522 	}
    523 
    524 	/* Do single-step fixup if needed. */
    525 	FIX_SSTEP(t);
    526 	KASSERT(lt != NULL);
    527 	lwp_addref(lt);
    528 
    529 	/*
    530 	 * Which locks do we need held? XXX Ugly.
    531 	 */
    532 	switch (req) {
    533 #ifdef PT_STEP
    534 	case PT_STEP:
    535 #endif
    536 	case PT_CONTINUE:
    537 	case PT_DETACH:
    538 	case PT_KILL:
    539 	case PT_SYSCALL:
    540 	case PT_SYSCALLEMU:
    541 	case PT_ATTACH:
    542 	case PT_TRACE_ME:
    543 		pheld = 1;
    544 		break;
    545 	default:
    546 		mutex_exit(proc_lock);
    547 		mutex_exit(t->p_lock);
    548 		pheld = 0;
    549 		break;
    550 	}
    551 
    552 	/* Now do the operation. */
    553 	write = 0;
    554 	*retval = 0;
    555 	tmp = 0;
    556 	resume_all = 1;
    557 
    558 	switch (req) {
    559 	case  PT_TRACE_ME:
    560 		/* Just set the trace flag. */
    561 		SET(t->p_slflag, PSL_TRACED);
    562 		t->p_opptr = t->p_pptr;
    563 		break;
    564 
    565 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
    566 	case  PT_WRITE_D:
    567 #if defined(__HAVE_RAS)
    568 		/*
    569 		 * Can't write to a RAS
    570 		 */
    571 		if (ras_lookup(t, addr) != (void *)-1) {
    572 			error = EACCES;
    573 			break;
    574 		}
    575 #endif
    576 		write = 1;
    577 		tmp = data;
    578 		/* FALLTHROUGH */
    579 
    580 	case  PT_READ_I:		/* XXX no separate I and D spaces */
    581 	case  PT_READ_D:
    582 		/* write = 0 done above. */
    583 		iov.iov_base = (void *)&tmp;
    584 		iov.iov_len = sizeof(tmp);
    585 		uio.uio_iov = &iov;
    586 		uio.uio_iovcnt = 1;
    587 		uio.uio_offset = (off_t)(unsigned long)addr;
    588 		uio.uio_resid = sizeof(tmp);
    589 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
    590 		UIO_SETUP_SYSSPACE(&uio);
    591 
    592 		error = process_domem(l, lt, &uio);
    593 		if (!write)
    594 			*retval = tmp;
    595 		break;
    596 
    597 	case  PT_IO:
    598 		error = ptm->ptm_copyinpiod(&piod, addr);
    599 		if (error)
    600 			break;
    601 
    602 		iov.iov_base = piod.piod_addr;
    603 		iov.iov_len = piod.piod_len;
    604 		uio.uio_iov = &iov;
    605 		uio.uio_iovcnt = 1;
    606 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
    607 		uio.uio_resid = piod.piod_len;
    608 
    609 		switch (piod.piod_op) {
    610 		case PIOD_READ_D:
    611 		case PIOD_READ_I:
    612 			uio.uio_rw = UIO_READ;
    613 			break;
    614 		case PIOD_WRITE_D:
    615 		case PIOD_WRITE_I:
    616 			/*
    617 			 * Can't write to a RAS
    618 			 */
    619 			if (ras_lookup(t, addr) != (void *)-1) {
    620 				return EACCES;
    621 			}
    622 			uio.uio_rw = UIO_WRITE;
    623 			break;
    624 		case PIOD_READ_AUXV:
    625 			req = PT_READ_D;
    626 			uio.uio_rw = UIO_READ;
    627 			tmp = t->p_execsw->es_arglen;
    628 			if (uio.uio_offset > tmp)
    629 				return EIO;
    630 			if (uio.uio_resid > tmp - uio.uio_offset)
    631 				uio.uio_resid = tmp - uio.uio_offset;
    632 			piod.piod_len = iov.iov_len = uio.uio_resid;
    633 			error = process_auxv_offset(t, &uio);
    634 			break;
    635 		default:
    636 			error = EINVAL;
    637 			break;
    638 		}
    639 		if (error)
    640 			break;
    641 		error = proc_vmspace_getref(l->l_proc, &vm);
    642 		if (error)
    643 			break;
    644 		uio.uio_vmspace = vm;
    645 
    646 		error = process_domem(l, lt, &uio);
    647 		piod.piod_len -= uio.uio_resid;
    648 		(void) ptm->ptm_copyoutpiod(&piod, addr);
    649 
    650 		uvmspace_free(vm);
    651 		break;
    652 
    653 	case  PT_DUMPCORE:
    654 		if ((path = addr) != NULL) {
    655 			char *dst;
    656 			len = data;
    657 
    658 			if (len < 0 || len >= MAXPATHLEN) {
    659 				error = EINVAL;
    660 				break;
    661 			}
    662 			dst = kmem_alloc(len + 1, KM_SLEEP);
    663 			if ((error = copyin(path, dst, len)) != 0) {
    664 				kmem_free(dst, len + 1);
    665 				break;
    666 			}
    667 			path = dst;
    668 			path[len] = '\0';
    669 		}
    670 		error = (*coredump_vec)(lt, path);
    671 		if (path)
    672 			kmem_free(path, len + 1);
    673 		break;
    674 
    675 #ifdef PT_STEP
    676 	case  PT_STEP:
    677 		/*
    678 		 * From the 4.4BSD PRM:
    679 		 * "Execution continues as in request PT_CONTINUE; however
    680 		 * as soon as possible after execution of at least one
    681 		 * instruction, execution stops again. [ ... ]"
    682 		 */
    683 #endif
    684 	case  PT_CONTINUE:
    685 	case  PT_SYSCALL:
    686 	case  PT_DETACH:
    687 		if (req == PT_SYSCALL) {
    688 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
    689 				SET(t->p_slflag, PSL_SYSCALL);
    690 #ifdef __HAVE_SYSCALL_INTERN
    691 				(*t->p_emul->e_syscall_intern)(t);
    692 #endif
    693 			}
    694 		} else {
    695 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
    696 				CLR(t->p_slflag, PSL_SYSCALL);
    697 #ifdef __HAVE_SYSCALL_INTERN
    698 				(*t->p_emul->e_syscall_intern)(t);
    699 #endif
    700 			}
    701 		}
    702 		t->p_trace_enabled = trace_is_enabled(t);
    703 
    704 		/*
    705 		 * Pick up the LWPID, if supplied.  There are two cases:
    706 		 * data < 0 : step or continue single thread, lwp = -data
    707 		 * data > 0 in PT_STEP : step this thread, continue others
    708 		 * For operations other than PT_STEP, data > 0 means
    709 		 * data is the signo to deliver to the process.
    710 		 */
    711 		tmp = data;
    712 		if (tmp >= 0) {
    713 #ifdef PT_STEP
    714 			if (req == PT_STEP)
    715 				signo = 0;
    716 			else
    717 #endif
    718 			{
    719 				signo = tmp;
    720 				tmp = 0;	/* don't search for LWP */
    721 			}
    722 		} else
    723 			tmp = -tmp;
    724 
    725 		if (tmp > 0) {
    726 			if (req == PT_DETACH) {
    727 				error = EINVAL;
    728 				break;
    729 			}
    730 			lwp_delref2 (lt);
    731 			lt = lwp_find(t, tmp);
    732 			if (lt == NULL) {
    733 				error = ESRCH;
    734 				break;
    735 			}
    736 			lwp_addref(lt);
    737 			resume_all = 0;
    738 			signo = 0;
    739 		}
    740 
    741 		/*
    742 		 * From the 4.4BSD PRM:
    743 		 * "The data argument is taken as a signal number and the
    744 		 * child's execution continues at location addr as if it
    745 		 * incurred that signal.  Normally the signal number will
    746 		 * be either 0 to indicate that the signal that caused the
    747 		 * stop should be ignored, or that value fetched out of
    748 		 * the process's image indicating which signal caused
    749 		 * the stop.  If addr is (int *)1 then execution continues
    750 		 * from where it stopped."
    751 		 */
    752 
    753 		/* Check that the data is a valid signal number or zero. */
    754 		if (signo < 0 || signo >= NSIG) {
    755 			error = EINVAL;
    756 			break;
    757 		}
    758 
    759 		/* Prevent process deadlock */
    760 		if (resume_all) {
    761 #ifdef PT_STEP
    762 			if (req == PT_STEP) {
    763 				if (lt->l_flag & LW_WSUSPEND) {
    764 					error = EDEADLK;
    765 					break;
    766 				}
    767 			} else
    768 #endif
    769 			{
    770 				error = EDEADLK;
    771 				LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    772 					if ((lt2->l_flag & LW_WSUSPEND) == 0) {
    773 						error = 0;
    774 						break;
    775 					}
    776 				}
    777 				if (error != 0)
    778 					break;
    779 			}
    780 		} else {
    781 			if (lt->l_flag & LW_WSUSPEND) {
    782 				error = EDEADLK;
    783 				break;
    784 			}
    785 		}
    786 
    787 		/* If the address parameter is not (int *)1, set the pc. */
    788 		if ((int *)addr != (int *)1) {
    789 			error = process_set_pc(lt, addr);
    790 			if (error != 0)
    791 				break;
    792 		}
    793 #ifdef PT_STEP
    794 		/*
    795 		 * Arrange for a single-step, if that's requested and possible.
    796 		 * More precisely, set the single step status as requested for
    797 		 * the requested thread, and clear it for other threads.
    798 		 */
    799 		LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    800 			if (ISSET(lt2->l_pflag, LP_SINGLESTEP)) {
    801 				lwp_lock(lt2);
    802 				process_sstep(lt2, 1);
    803 				lwp_unlock(lt2);
    804 			} else if (lt != lt2) {
    805 				lwp_lock(lt2);
    806 				process_sstep(lt2, 0);
    807 				lwp_unlock(lt2);
    808 			}
    809 		}
    810 		error = process_sstep(lt,
    811 		    ISSET(lt->l_pflag, LP_SINGLESTEP) || req == PT_STEP);
    812 		if (error)
    813 			break;
    814 #endif
    815 		if (req == PT_DETACH) {
    816 			CLR(t->p_slflag, PSL_TRACED|PSL_SYSCALL);
    817 
    818 			/* give process back to original parent or init */
    819 			if (t->p_opptr != t->p_pptr) {
    820 				struct proc *pp = t->p_opptr;
    821 				proc_reparent(t, pp ? pp : initproc);
    822 			}
    823 
    824 			/* not being traced any more */
    825 			t->p_opptr = NULL;
    826 
    827 			/* clear single step */
    828 			LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    829 				CLR(lt2->l_pflag, LP_SINGLESTEP);
    830 			}
    831 			CLR(lt->l_pflag, LP_SINGLESTEP);
    832 		}
    833 	sendsig:
    834 		t->p_fpid = 0;
    835 		t->p_vfpid = 0;
    836 		t->p_vfpid_done = 0;
    837 		t->p_lwp_created = 0;
    838 		t->p_lwp_exited = 0;
    839 		/* Finally, deliver the requested signal (or none). */
    840 		if (t->p_stat == SSTOP) {
    841 			/*
    842 			 * Unstop the process.  If it needs to take a
    843 			 * signal, make all efforts to ensure that at
    844 			 * an LWP runs to see it.
    845 			 */
    846 			t->p_xsig = signo;
    847 			if (resume_all)
    848 				proc_unstop(t);
    849 			else
    850 				lwp_unstop(lt);
    851 		} else if (t->p_sigctx.ps_faked) {
    852 			if (signo != t->p_sigctx.ps_info._signo) {
    853 				error = EINVAL;
    854 				break;
    855 			}
    856 			t->p_sigctx.ps_faked = false;
    857 			KSI_INIT_EMPTY(&ksi);
    858 			ksi.ksi_info = t->p_sigctx.ps_info;
    859 			ksi.ksi_lid = t->p_sigctx.ps_lwp;
    860 			kpsignal2(t, &ksi);
    861 		} else if (signo != 0) {
    862 			KSI_INIT_EMPTY(&ksi);
    863 			ksi.ksi_signo = signo;
    864 			kpsignal2(t, &ksi);
    865 		}
    866 		break;
    867 
    868 	case  PT_SYSCALLEMU:
    869 		if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
    870 			error = EINVAL;
    871 			break;
    872 		}
    873 		SET(t->p_slflag, PSL_SYSCALLEMU);
    874 		break;
    875 
    876 #ifdef PT_STEP
    877 	case  PT_SETSTEP:
    878 		write = 1;
    879 
    880 	case  PT_CLEARSTEP:
    881 		/* write = 0 done above. */
    882 
    883 		tmp = data;
    884 		if (tmp != 0 && t->p_nlwps > 1) {
    885 			lwp_delref(lt);
    886 			mutex_enter(t->p_lock);
    887 			lt = lwp_find(t, tmp);
    888 			if (lt == NULL) {
    889 				mutex_exit(t->p_lock);
    890 				error = ESRCH;
    891 				break;
    892 			}
    893 			lwp_addref(lt);
    894 			mutex_exit(t->p_lock);
    895 		}
    896 
    897 		if (ISSET(lt->l_flag, LW_SYSTEM))
    898 			error = EINVAL;
    899 		else if (write)
    900 			SET(lt->l_pflag, LP_SINGLESTEP);
    901 		else
    902 			CLR(lt->l_pflag, LP_SINGLESTEP);
    903 		break;
    904 #endif
    905 
    906 	case  PT_KILL:
    907 		/* just send the process a KILL signal. */
    908 		signo = SIGKILL;
    909 		goto sendsig;	/* in PT_CONTINUE, above. */
    910 
    911 	case  PT_ATTACH:
    912 		/*
    913 		 * Go ahead and set the trace flag.
    914 		 * Save the old parent (it's reset in
    915 		 *   _DETACH, and also in kern_exit.c:wait4()
    916 		 * Reparent the process so that the tracing
    917 		 *   proc gets to see all the action.
    918 		 * Stop the target.
    919 		 */
    920 		proc_changeparent(t, p);
    921 		signo = SIGSTOP;
    922 		goto sendsig;
    923 
    924 	case  PT_GET_EVENT_MASK:
    925 		if (data != sizeof(pe)) {
    926 			DPRINTF(("ptrace(%d): %d != %zu\n", req,
    927 			    data, sizeof(pe)));
    928 			error = EINVAL;
    929 			break;
    930 		}
    931 		memset(&pe, 0, sizeof(pe));
    932 		pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
    933 		    PTRACE_FORK : 0;
    934 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
    935 		    PTRACE_VFORK : 0;
    936 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
    937 		    PTRACE_VFORK_DONE : 0;
    938 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
    939 		    PTRACE_LWP_CREATE : 0;
    940 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
    941 		    PTRACE_LWP_EXIT : 0;
    942 		error = copyout(&pe, addr, sizeof(pe));
    943 		break;
    944 
    945 	case  PT_SET_EVENT_MASK:
    946 		if (data != sizeof(pe)) {
    947 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    948 			    sizeof(pe)));
    949 			error = EINVAL;
    950 			break;
    951 		}
    952 		if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
    953 			return error;
    954 		if (pe.pe_set_event & PTRACE_FORK)
    955 			SET(t->p_slflag, PSL_TRACEFORK);
    956 		else
    957 			CLR(t->p_slflag, PSL_TRACEFORK);
    958 #if notyet
    959 		if (pe.pe_set_event & PTRACE_VFORK)
    960 			SET(t->p_slflag, PSL_TRACEVFORK);
    961 		else
    962 			CLR(t->p_slflag, PSL_TRACEVFORK);
    963 #else
    964 		if (pe.pe_set_event & PTRACE_VFORK) {
    965 			error = ENOTSUP;
    966 			break;
    967 		}
    968 #endif
    969 		if (pe.pe_set_event & PTRACE_VFORK_DONE)
    970 			SET(t->p_slflag, PSL_TRACEVFORK_DONE);
    971 		else
    972 			CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
    973 		if (pe.pe_set_event & PTRACE_LWP_CREATE)
    974 			SET(t->p_slflag, PSL_TRACELWP_CREATE);
    975 		else
    976 			CLR(t->p_slflag, PSL_TRACELWP_CREATE);
    977 		if (pe.pe_set_event & PTRACE_LWP_EXIT)
    978 			SET(t->p_slflag, PSL_TRACELWP_EXIT);
    979 		else
    980 			CLR(t->p_slflag, PSL_TRACELWP_EXIT);
    981 		break;
    982 
    983 	case  PT_GET_PROCESS_STATE:
    984 		if (data != sizeof(ps)) {
    985 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    986 			    sizeof(ps)));
    987 			error = EINVAL;
    988 			break;
    989 		}
    990 		memset(&ps, 0, sizeof(ps));
    991 		if (t->p_fpid) {
    992 			ps.pe_report_event = PTRACE_FORK;
    993 			ps.pe_other_pid = t->p_fpid;
    994 		} else if (t->p_vfpid) {
    995 			ps.pe_report_event = PTRACE_VFORK;
    996 			ps.pe_other_pid = t->p_vfpid;
    997 		} else if (t->p_vfpid_done) {
    998 			ps.pe_report_event = PTRACE_VFORK_DONE;
    999 			ps.pe_other_pid = t->p_vfpid_done;
   1000 		} else if (t->p_lwp_created) {
   1001 			ps.pe_report_event = PTRACE_LWP_CREATE;
   1002 			ps.pe_lwp = t->p_lwp_created;
   1003 		} else if (t->p_lwp_exited) {
   1004 			ps.pe_report_event = PTRACE_LWP_EXIT;
   1005 			ps.pe_lwp = t->p_lwp_exited;
   1006 		}
   1007 		error = copyout(&ps, addr, sizeof(ps));
   1008 		break;
   1009 
   1010 	case PT_LWPINFO:
   1011 		if (data != sizeof(pl)) {
   1012 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1013 			    sizeof(pl)));
   1014 			error = EINVAL;
   1015 			break;
   1016 		}
   1017 		error = copyin(addr, &pl, sizeof(pl));
   1018 		if (error)
   1019 			break;
   1020 		tmp = pl.pl_lwpid;
   1021 		lwp_delref(lt);
   1022 		mutex_enter(t->p_lock);
   1023 		if (tmp == 0)
   1024 			lt = lwp_find_first(t);
   1025 		else {
   1026 			lt = lwp_find(t, tmp);
   1027 			if (lt == NULL) {
   1028 				mutex_exit(t->p_lock);
   1029 				error = ESRCH;
   1030 				break;
   1031 			}
   1032 			lt = LIST_NEXT(lt, l_sibling);
   1033 		}
   1034 		while (lt != NULL && !lwp_alive(lt))
   1035 			lt = LIST_NEXT(lt, l_sibling);
   1036 		pl.pl_lwpid = 0;
   1037 		pl.pl_event = 0;
   1038 		if (lt) {
   1039 			lwp_addref(lt);
   1040 			pl.pl_lwpid = lt->l_lid;
   1041 
   1042 			if (lt->l_flag & LW_WSUSPEND)
   1043 				pl.pl_event = PL_EVENT_SUSPENDED;
   1044 			/*
   1045 			 * If we match the lwp, or it was sent to every lwp,
   1046 			 * we set PL_EVENT_SIGNAL.
   1047 			 * XXX: ps_lwp == 0 means everyone and noone, so
   1048 			 * check ps_signo too.
   1049 			 */
   1050 			else if (lt->l_lid == t->p_sigctx.ps_lwp
   1051 			         || (t->p_sigctx.ps_lwp == 0 &&
   1052 			             t->p_sigctx.ps_info._signo))
   1053 				pl.pl_event = PL_EVENT_SIGNAL;
   1054 		}
   1055 		mutex_exit(t->p_lock);
   1056 
   1057 		error = copyout(&pl, addr, sizeof(pl));
   1058 		break;
   1059 
   1060 	case  PT_SET_SIGINFO:
   1061 		if (data != sizeof(psi)) {
   1062 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1063 			    sizeof(psi)));
   1064 			error = EINVAL;
   1065 			break;
   1066 		}
   1067 
   1068 		error = copyin(addr, &psi, sizeof(psi));
   1069 		if (error)
   1070 			break;
   1071 
   1072 		/* Check that the data is a valid signal number or zero. */
   1073 		if (psi.psi_siginfo.si_signo < 0 ||
   1074 		    psi.psi_siginfo.si_signo >= NSIG) {
   1075 			error = EINVAL;
   1076 			break;
   1077 		}
   1078 
   1079 		tmp = psi.psi_lwpid;
   1080 		if (tmp != 0)
   1081 			lwp_delref(lt);
   1082 
   1083 		mutex_enter(t->p_lock);
   1084 
   1085 		if (tmp != 0) {
   1086 			lt = lwp_find(t, tmp);
   1087 			if (lt == NULL) {
   1088 				mutex_exit(t->p_lock);
   1089 				error = ESRCH;
   1090 				break;
   1091 			}
   1092 			lwp_addref(lt);
   1093 		}
   1094 
   1095 		t->p_sigctx.ps_faked = true;
   1096 		t->p_sigctx.ps_info = psi.psi_siginfo._info;
   1097 		t->p_sigctx.ps_lwp = psi.psi_lwpid;
   1098 		mutex_exit(t->p_lock);
   1099 		break;
   1100 
   1101 	case  PT_GET_SIGINFO:
   1102 		if (data != sizeof(psi)) {
   1103 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1104 			    sizeof(psi)));
   1105 			error = EINVAL;
   1106 			break;
   1107 		}
   1108 		mutex_enter(t->p_lock);
   1109 		psi.psi_siginfo._info = t->p_sigctx.ps_info;
   1110 		psi.psi_lwpid = t->p_sigctx.ps_lwp;
   1111 		mutex_exit(t->p_lock);
   1112 
   1113 		error = copyout(&psi, addr, sizeof(psi));
   1114 		if (error)
   1115 			break;
   1116 
   1117 		break;
   1118 
   1119 	case  PT_SET_SIGMASK:
   1120 		write = 1;
   1121 
   1122 	case  PT_GET_SIGMASK:
   1123 		/* write = 0 done above. */
   1124 
   1125 		tmp = data;
   1126 		if (tmp != 0 && t->p_nlwps > 1) {
   1127 			lwp_delref(lt);
   1128 			mutex_enter(t->p_lock);
   1129 			lt = lwp_find(t, tmp);
   1130 			if (lt == NULL) {
   1131 				mutex_exit(t->p_lock);
   1132 				error = ESRCH;
   1133 				break;
   1134 			}
   1135 			lwp_addref(lt);
   1136 			mutex_exit(t->p_lock);
   1137 		}
   1138 
   1139 		if (lt->l_flag & LW_SYSTEM)
   1140 			error = EINVAL;
   1141 		else if (write == 1) {
   1142 			error = copyin(addr, &lt->l_sigmask, sizeof(sigset_t));
   1143 			sigminusset(&sigcantmask, &lt->l_sigmask);
   1144 		} else
   1145 			error = copyout(&lt->l_sigmask, addr, sizeof(sigset_t));
   1146 
   1147 		break;
   1148 
   1149 	case  PT_RESUME:
   1150 		write = 1;
   1151 
   1152 	case  PT_SUSPEND:
   1153 		/* write = 0 done above. */
   1154 
   1155 		tmp = data;
   1156 		if (tmp != 0 && t->p_nlwps > 1) {
   1157 			lwp_delref(lt);
   1158 			mutex_enter(t->p_lock);
   1159 			lt = lwp_find(t, tmp);
   1160 			if (lt == NULL) {
   1161 				mutex_exit(t->p_lock);
   1162 				error = ESRCH;
   1163 				break;
   1164 			}
   1165 			lwp_addref(lt);
   1166 			mutex_exit(t->p_lock);
   1167 		}
   1168 		if (lt->l_flag & LW_SYSTEM) {
   1169 			error = EINVAL;
   1170 		} else {
   1171 			lwp_lock(lt);
   1172 			if (write == 0)
   1173 				lt->l_flag |= LW_WSUSPEND;
   1174 			else
   1175 				lt->l_flag &= ~LW_WSUSPEND;
   1176 			lwp_unlock(lt);
   1177 		}
   1178 		break;
   1179 
   1180 #ifdef PT_SETREGS
   1181 	case  PT_SETREGS:
   1182 		write = 1;
   1183 #endif
   1184 #ifdef PT_GETREGS
   1185 	case  PT_GETREGS:
   1186 		/* write = 0 done above. */
   1187 #endif
   1188 #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1189 		tmp = data;
   1190 		if (tmp != 0 && t->p_nlwps > 1) {
   1191 			lwp_delref(lt);
   1192 			mutex_enter(t->p_lock);
   1193 			lt = lwp_find(t, tmp);
   1194 			if (lt == NULL) {
   1195 				mutex_exit(t->p_lock);
   1196 				error = ESRCH;
   1197 				break;
   1198 			}
   1199 			lwp_addref(lt);
   1200 			mutex_exit(t->p_lock);
   1201 		}
   1202 		if (!process_validregs(lt))
   1203 			error = EINVAL;
   1204 		else {
   1205 			error = proc_vmspace_getref(p, &vm);
   1206 			if (error)
   1207 				break;
   1208 			iov.iov_base = addr;
   1209 			iov.iov_len = PROC_REGSZ(p);
   1210 			uio.uio_iov = &iov;
   1211 			uio.uio_iovcnt = 1;
   1212 			uio.uio_offset = 0;
   1213 			uio.uio_resid = iov.iov_len;
   1214 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1215 			uio.uio_vmspace = vm;
   1216 
   1217 			error = ptm->ptm_doregs(l, lt, &uio);
   1218 			uvmspace_free(vm);
   1219 		}
   1220 		break;
   1221 #endif
   1222 
   1223 #ifdef PT_SETFPREGS
   1224 	case  PT_SETFPREGS:
   1225 		write = 1;
   1226 		/*FALLTHROUGH*/
   1227 #endif
   1228 #ifdef PT_GETFPREGS
   1229 	case  PT_GETFPREGS:
   1230 		/* write = 0 done above. */
   1231 #endif
   1232 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1233 		tmp = data;
   1234 		if (tmp != 0 && t->p_nlwps > 1) {
   1235 			lwp_delref(lt);
   1236 			mutex_enter(t->p_lock);
   1237 			lt = lwp_find(t, tmp);
   1238 			if (lt == NULL) {
   1239 				mutex_exit(t->p_lock);
   1240 				error = ESRCH;
   1241 				break;
   1242 			}
   1243 			lwp_addref(lt);
   1244 			mutex_exit(t->p_lock);
   1245 		}
   1246 		if (!process_validfpregs(lt))
   1247 			error = EINVAL;
   1248 		else {
   1249 			error = proc_vmspace_getref(p, &vm);
   1250 			if (error)
   1251 				break;
   1252 			iov.iov_base = addr;
   1253 			iov.iov_len = PROC_FPREGSZ(p);
   1254 			uio.uio_iov = &iov;
   1255 			uio.uio_iovcnt = 1;
   1256 			uio.uio_offset = 0;
   1257 			uio.uio_resid = iov.iov_len;
   1258 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1259 			uio.uio_vmspace = vm;
   1260 
   1261 			error = ptm->ptm_dofpregs(l, lt, &uio);
   1262 			uvmspace_free(vm);
   1263 		}
   1264 		break;
   1265 #endif
   1266 
   1267 #ifdef PT_SETDBREGS
   1268 	case  PT_SETDBREGS:
   1269 		write = 1;
   1270 		/*FALLTHROUGH*/
   1271 #endif
   1272 #ifdef PT_GETDBREGS
   1273 	case  PT_GETDBREGS:
   1274 		/* write = 0 done above. */
   1275 #endif
   1276 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1277 		tmp = data;
   1278 		if (tmp != 0 && t->p_nlwps > 1) {
   1279 			lwp_delref(lt);
   1280 			mutex_enter(t->p_lock);
   1281 			lt = lwp_find(t, tmp);
   1282 			if (lt == NULL) {
   1283 				mutex_exit(t->p_lock);
   1284 				error = ESRCH;
   1285 				break;
   1286 			}
   1287 			lwp_addref(lt);
   1288 			mutex_exit(t->p_lock);
   1289 		}
   1290 		if (!process_validdbregs(lt))
   1291 			error = EINVAL;
   1292 		else {
   1293 			error = proc_vmspace_getref(p, &vm);
   1294 			if (error)
   1295 				break;
   1296 			iov.iov_base = addr;
   1297 			iov.iov_len = PROC_DBREGSZ(p);
   1298 			uio.uio_iov = &iov;
   1299 			uio.uio_iovcnt = 1;
   1300 			uio.uio_offset = 0;
   1301 			uio.uio_resid = iov.iov_len;
   1302 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1303 			uio.uio_vmspace = vm;
   1304 
   1305 			error = ptm->ptm_dodbregs(l, lt, &uio);
   1306 			uvmspace_free(vm);
   1307 		}
   1308 		break;
   1309 #endif
   1310 
   1311 #ifdef __HAVE_PTRACE_MACHDEP
   1312 	PTRACE_MACHDEP_REQUEST_CASES
   1313 		error = ptrace_machdep_dorequest(l, lt, req, addr, data);
   1314 		break;
   1315 #endif
   1316 	}
   1317 
   1318 	if (pheld) {
   1319 		mutex_exit(t->p_lock);
   1320 		mutex_exit(proc_lock);
   1321 	}
   1322 	if (lt != NULL)
   1323 		lwp_delref(lt);
   1324 	rw_exit(&t->p_reflock);
   1325 
   1326 	return error;
   1327 }
   1328 
   1329 typedef int (*regfunc_t)(struct lwp *, void *);
   1330 
   1331 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS) || \
   1332     defined(PT_GETFPREGS) || defined(PT_SETFPREGS) || \
   1333     defined(PT_GETREGS) || defined(PT_SETREGS)
   1334 static int
   1335 proc_regio(struct lwp *l, struct uio *uio, size_t kl, regfunc_t r, regfunc_t w)
   1336 {
   1337 	char buf[1024];
   1338 	int error;
   1339 	char *kv;
   1340 
   1341 	if (kl > sizeof(buf))
   1342 		return E2BIG;
   1343 
   1344 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)kl)
   1345 		return EINVAL;
   1346 
   1347 	kv = buf + uio->uio_offset;
   1348 	kl -= uio->uio_offset;
   1349 
   1350 	if (kl > uio->uio_resid)
   1351 		kl = uio->uio_resid;
   1352 
   1353 	error = (*r)(l, buf);
   1354 	if (error == 0)
   1355 		error = uiomove(kv, kl, uio);
   1356 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1357 		if (l->l_stat != LSSTOP)
   1358 			error = EBUSY;
   1359 		else
   1360 			error = (*w)(l, buf);
   1361 	}
   1362 
   1363 	uio->uio_offset = 0;
   1364 	return error;
   1365 }
   1366 #endif
   1367 
   1368 int
   1369 process_doregs(struct lwp *curl /*tracer*/,
   1370     struct lwp *l /*traced*/,
   1371     struct uio *uio)
   1372 {
   1373 #if defined(PT_GETREGS) || defined(PT_SETREGS)
   1374 	size_t s;
   1375 	regfunc_t r, w;
   1376 
   1377 #ifdef COMPAT_NETBSD32
   1378 	const bool pk32 = (l->l_proc->p_flag & PK_32) != 0;
   1379 
   1380 	if (__predict_false(pk32)) {
   1381 		s = sizeof(process_reg32);
   1382 		r = (regfunc_t)process_read_regs32;
   1383 		w = (regfunc_t)process_write_regs32;
   1384 	} else
   1385 #endif
   1386 	{
   1387 		s = sizeof(struct reg);
   1388 		r = (regfunc_t)process_read_regs;
   1389 		w = (regfunc_t)process_write_regs;
   1390 	}
   1391 	return proc_regio(l, uio, s, r, w);
   1392 #else
   1393 	return EINVAL;
   1394 #endif
   1395 }
   1396 
   1397 int
   1398 process_validregs(struct lwp *l)
   1399 {
   1400 
   1401 #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1402 	return (l->l_flag & LW_SYSTEM) == 0;
   1403 #else
   1404 	return 0;
   1405 #endif
   1406 }
   1407 
   1408 int
   1409 process_dofpregs(struct lwp *curl /*tracer*/,
   1410     struct lwp *l /*traced*/,
   1411     struct uio *uio)
   1412 {
   1413 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
   1414 	size_t s;
   1415 	regfunc_t r, w;
   1416 
   1417 #ifdef COMPAT_NETBSD32
   1418 	const bool pk32 = (l->l_proc->p_flag & PK_32) != 0;
   1419 
   1420 	if (__predict_false(pk32)) {
   1421 		s = sizeof(process_fpreg32);
   1422 		r = (regfunc_t)process_read_fpregs32;
   1423 		w = (regfunc_t)process_write_fpregs32;
   1424 	} else
   1425 #endif
   1426 	{
   1427 		s = sizeof(struct fpreg);
   1428 		r = (regfunc_t)process_read_fpregs;
   1429 		w = (regfunc_t)process_write_fpregs;
   1430 	}
   1431 	return proc_regio(l, uio, s, r, w);
   1432 #else
   1433 	return EINVAL;
   1434 #endif
   1435 }
   1436 
   1437 int
   1438 process_validfpregs(struct lwp *l)
   1439 {
   1440 
   1441 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1442 	return (l->l_flag & LW_SYSTEM) == 0;
   1443 #else
   1444 	return 0;
   1445 #endif
   1446 }
   1447 
   1448 int
   1449 process_dodbregs(struct lwp *curl /*tracer*/,
   1450     struct lwp *l /*traced*/,
   1451     struct uio *uio)
   1452 {
   1453 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
   1454 	size_t s;
   1455 	regfunc_t r, w;
   1456 
   1457 #ifdef COMPAT_NETBSD32
   1458 	const bool pk32 = (l->l_proc->p_flag & PK_32) != 0;
   1459 
   1460 	if (__predict_false(pk32)) {
   1461 		s = sizeof(process_dbreg32);
   1462 		r = (regfunc_t)process_read_dbregs32;
   1463 		w = (regfunc_t)process_write_dbregs32;
   1464 	} else
   1465 #endif
   1466 	{
   1467 		s = sizeof(struct dbreg);
   1468 		r = (regfunc_t)process_read_dbregs;
   1469 		w = (regfunc_t)process_write_dbregs;
   1470 	}
   1471 	return proc_regio(l, uio, s, r, w);
   1472 #else
   1473 	return EINVAL;
   1474 #endif
   1475 }
   1476 
   1477 int
   1478 process_validdbregs(struct lwp *l)
   1479 {
   1480 
   1481 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1482 	return (l->l_flag & LW_SYSTEM) == 0;
   1483 #else
   1484 	return 0;
   1485 #endif
   1486 }
   1487 
   1488 static int
   1489 process_auxv_offset(struct proc *p, struct uio *uio)
   1490 {
   1491 	struct ps_strings pss;
   1492 	int error;
   1493 	off_t off = (off_t)p->p_psstrp;
   1494 
   1495 	if ((error = copyin_psstrings(p, &pss)) != 0)
   1496 		return error;
   1497 
   1498 	if (pss.ps_envstr == NULL)
   1499 		return EIO;
   1500 
   1501 	uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
   1502 #ifdef __MACHINE_STACK_GROWS_UP
   1503 	if (uio->uio_offset < off)
   1504 		return EIO;
   1505 #else
   1506 	if (uio->uio_offset > off)
   1507 		return EIO;
   1508 	if ((uio->uio_offset + uio->uio_resid) > off)
   1509 		uio->uio_resid = off - uio->uio_offset;
   1510 #endif
   1511 	return 0;
   1512 }
   1513 #endif /* PTRACE */
   1514 
   1515 MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
   1516 
   1517 static int
   1518 ptrace_common_modcmd(modcmd_t cmd, void *arg)
   1519 {
   1520         int error;
   1521 
   1522         switch (cmd) {
   1523         case MODULE_CMD_INIT:
   1524                 error = ptrace_init();
   1525                 break;
   1526         case MODULE_CMD_FINI:
   1527                 error = ptrace_fini();
   1528                 break;
   1529         default:
   1530 		ptrace_hooks();
   1531                 error = ENOTTY;
   1532                 break;
   1533         }
   1534         return error;
   1535 }
   1536