Home | History | Annotate | Line # | Download | only in kern
sys_ptrace_common.c revision 1.22.2.1
      1 /*	$NetBSD: sys_ptrace_common.c,v 1.22.2.1 2018/03/06 09:52:09 martin 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.22.2.1 2018/03/06 09:52:09 martin Exp $");
    122 
    123 #ifdef _KERNEL_OPT
    124 #include "opt_ptrace.h"
    125 #include "opt_ktrace.h"
    126 #include "opt_pax.h"
    127 #endif
    128 
    129 #include <sys/param.h>
    130 #include <sys/systm.h>
    131 #include <sys/proc.h>
    132 #include <sys/errno.h>
    133 #include <sys/exec.h>
    134 #include <sys/pax.h>
    135 #include <sys/ptrace.h>
    136 #include <sys/uio.h>
    137 #include <sys/ras.h>
    138 #include <sys/kmem.h>
    139 #include <sys/kauth.h>
    140 #include <sys/mount.h>
    141 #include <sys/syscallargs.h>
    142 #include <sys/module.h>
    143 #include <sys/condvar.h>
    144 #include <sys/mutex.h>
    145 
    146 #include <uvm/uvm_extern.h>
    147 
    148 #include <machine/reg.h>
    149 
    150 #ifdef PTRACE
    151 
    152 # ifdef DEBUG
    153 #  define DPRINTF(a) uprintf a
    154 # else
    155 #  define DPRINTF(a)
    156 # endif
    157 
    158 static kauth_listener_t ptrace_listener;
    159 static int process_auxv_offset(struct proc *, struct uio *);
    160 
    161 #if 0
    162 static int ptrace_cbref;
    163 static kmutex_t ptrace_mtx;
    164 static kcondvar_t ptrace_cv;
    165 #endif
    166 
    167 static int
    168 ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    169     void *arg0, void *arg1, void *arg2, void *arg3)
    170 {
    171 	struct proc *p;
    172 	int result;
    173 
    174 	result = KAUTH_RESULT_DEFER;
    175 	p = arg0;
    176 
    177 #if 0
    178 	mutex_enter(&ptrace_mtx);
    179 	ptrace_cbref++;
    180 	mutex_exit(&ptrace_mtx);
    181 #endif
    182 	if (action != KAUTH_PROCESS_PTRACE)
    183 		goto out;
    184 
    185 	switch ((u_long)arg1) {
    186 	case PT_TRACE_ME:
    187 	case PT_ATTACH:
    188 	case PT_WRITE_I:
    189 	case PT_WRITE_D:
    190 	case PT_READ_I:
    191 	case PT_READ_D:
    192 	case PT_IO:
    193 #ifdef PT_GETREGS
    194 	case PT_GETREGS:
    195 #endif
    196 #ifdef PT_SETREGS
    197 	case PT_SETREGS:
    198 #endif
    199 #ifdef PT_GETFPREGS
    200 	case PT_GETFPREGS:
    201 #endif
    202 #ifdef PT_SETFPREGS
    203 	case PT_SETFPREGS:
    204 #endif
    205 #ifdef PT_GETDBREGS
    206 	case PT_GETDBREGS:
    207 #endif
    208 #ifdef PT_SETDBREGS
    209 	case PT_SETDBREGS:
    210 #endif
    211 	case PT_SET_EVENT_MASK:
    212 	case PT_GET_EVENT_MASK:
    213 	case PT_GET_PROCESS_STATE:
    214 	case PT_SET_SIGINFO:
    215 	case PT_GET_SIGINFO:
    216 #ifdef __HAVE_PTRACE_MACHDEP
    217 	PTRACE_MACHDEP_REQUEST_CASES
    218 #endif
    219 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    220 		    ISSET(p->p_flag, PK_SUGID)) {
    221 			break;
    222 		}
    223 
    224 		result = KAUTH_RESULT_ALLOW;
    225 
    226 	break;
    227 
    228 #ifdef PT_STEP
    229 	case PT_STEP:
    230 	case PT_SETSTEP:
    231 	case PT_CLEARSTEP:
    232 #endif
    233 	case PT_CONTINUE:
    234 	case PT_KILL:
    235 	case PT_DETACH:
    236 	case PT_LWPINFO:
    237 	case PT_SYSCALL:
    238 	case PT_SYSCALLEMU:
    239 	case PT_DUMPCORE:
    240 	case PT_RESUME:
    241 	case PT_SUSPEND:
    242 		result = KAUTH_RESULT_ALLOW;
    243 		break;
    244 
    245 	default:
    246 		break;
    247 	}
    248 
    249  out:
    250 #if 0
    251 	mutex_enter(&ptrace_mtx);
    252 	if (--ptrace_cbref == 0)
    253 		cv_broadcast(&ptrace_cv);
    254 	mutex_exit(&ptrace_mtx);
    255 #endif
    256 
    257 	return result;
    258 }
    259 
    260 int
    261 ptrace_init(void)
    262 {
    263 
    264 #if 0
    265 	mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
    266 	cv_init(&ptrace_cv, "ptracecb");
    267 	ptrace_cbref = 0;
    268 #endif
    269 	ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    270 	    ptrace_listener_cb, NULL);
    271 	return 0;
    272 }
    273 
    274 int
    275 ptrace_fini(void)
    276 {
    277 
    278 	kauth_unlisten_scope(ptrace_listener);
    279 
    280 #if 0
    281 	/* Make sure no-one is executing our kauth listener */
    282 
    283 	mutex_enter(&ptrace_mtx);
    284 	while (ptrace_cbref != 0)
    285 		cv_wait(&ptrace_cv, &ptrace_mtx);
    286 	mutex_exit(&ptrace_mtx);
    287 	mutex_destroy(&ptrace_mtx);
    288 	cv_destroy(&ptrace_cv);
    289 #endif
    290 
    291 	return 0;
    292 }
    293 
    294 int
    295 do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
    296     void *addr, int data, register_t *retval)
    297 {
    298 	struct proc *p = l->l_proc;
    299 	struct lwp *lt;
    300 	struct lwp *lt2;
    301 	struct proc *t;				/* target process */
    302 	struct uio uio;
    303 	struct iovec iov;
    304 	struct ptrace_io_desc piod;
    305 	struct ptrace_event pe;
    306 	struct ptrace_state ps;
    307 	struct ptrace_lwpinfo pl;
    308 	struct ptrace_siginfo psi;
    309 	struct vmspace *vm;
    310 	int error, write, tmp, pheld;
    311 	int signo = 0;
    312 	int resume_all;
    313 	ksiginfo_t ksi;
    314 	char *path;
    315 	int len = 0;
    316 	error = 0;
    317 
    318 	/*
    319 	 * If attaching or detaching, we need to get a write hold on the
    320 	 * proclist lock so that we can re-parent the target process.
    321 	 */
    322 	mutex_enter(proc_lock);
    323 
    324 	/* "A foolish consistency..." XXX */
    325 	if (req == PT_TRACE_ME) {
    326 		t = p;
    327 		mutex_enter(t->p_lock);
    328 	} else {
    329 		/* Find the process we're supposed to be operating on. */
    330 		t = proc_find(pid);
    331 		if (t == NULL) {
    332 			mutex_exit(proc_lock);
    333 			return ESRCH;
    334 		}
    335 
    336 		/* XXX-elad */
    337 		mutex_enter(t->p_lock);
    338 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
    339 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    340 		if (error) {
    341 			mutex_exit(proc_lock);
    342 			mutex_exit(t->p_lock);
    343 			return ESRCH;
    344 		}
    345 	}
    346 
    347 	/*
    348 	 * Grab a reference on the process to prevent it from execing or
    349 	 * exiting.
    350 	 */
    351 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
    352 		mutex_exit(proc_lock);
    353 		mutex_exit(t->p_lock);
    354 		return EBUSY;
    355 	}
    356 
    357 	/* Make sure we can operate on it. */
    358 	switch (req) {
    359 	case  PT_TRACE_ME:
    360 		/* Saying that you're being traced is always legal. */
    361 		break;
    362 
    363 	case  PT_ATTACH:
    364 		/*
    365 		 * You can't attach to a process if:
    366 		 *	(1) it's the process that's doing the attaching,
    367 		 */
    368 		if (t->p_pid == p->p_pid) {
    369 			error = EINVAL;
    370 			break;
    371 		}
    372 
    373 		/*
    374 		 *  (2) it's a system process
    375 		 */
    376 		if (t->p_flag & PK_SYSTEM) {
    377 			error = EPERM;
    378 			break;
    379 		}
    380 
    381 		/*
    382 		 *	(3) it's already being traced, or
    383 		 */
    384 		if (ISSET(t->p_slflag, PSL_TRACED)) {
    385 			error = EBUSY;
    386 			break;
    387 		}
    388 
    389 		/*
    390 		 * 	(4) the tracer is chrooted, and its root directory is
    391 		 * 	    not at or above the root directory of the tracee
    392 		 */
    393 		mutex_exit(t->p_lock);	/* XXXSMP */
    394 		tmp = proc_isunder(t, l);
    395 		mutex_enter(t->p_lock);	/* XXXSMP */
    396 		if (!tmp) {
    397 			error = EPERM;
    398 			break;
    399 		}
    400 		break;
    401 
    402 	case  PT_READ_I:
    403 	case  PT_READ_D:
    404 	case  PT_WRITE_I:
    405 	case  PT_WRITE_D:
    406 	case  PT_IO:
    407 	case  PT_SET_SIGINFO:
    408 	case  PT_GET_SIGINFO:
    409 #ifdef PT_GETREGS
    410 	case  PT_GETREGS:
    411 #endif
    412 #ifdef PT_SETREGS
    413 	case  PT_SETREGS:
    414 #endif
    415 #ifdef PT_GETFPREGS
    416 	case  PT_GETFPREGS:
    417 #endif
    418 #ifdef PT_SETFPREGS
    419 	case  PT_SETFPREGS:
    420 #endif
    421 #ifdef PT_GETDBREGS
    422 	case  PT_GETDBREGS:
    423 #endif
    424 #ifdef PT_SETDBREGS
    425 	case  PT_SETDBREGS:
    426 #endif
    427 #ifdef __HAVE_PTRACE_MACHDEP
    428 	PTRACE_MACHDEP_REQUEST_CASES
    429 #endif
    430 		/*
    431 		 * You can't read/write the memory or registers of a process
    432 		 * if the tracer is chrooted, and its root directory is not at
    433 		 * or above the root directory of the tracee.
    434 		 */
    435 		mutex_exit(t->p_lock);	/* XXXSMP */
    436 		tmp = proc_isunder(t, l);
    437 		mutex_enter(t->p_lock);	/* XXXSMP */
    438 		if (!tmp) {
    439 			error = EPERM;
    440 			break;
    441 		}
    442 		/*FALLTHROUGH*/
    443 
    444 	case  PT_CONTINUE:
    445 	case  PT_KILL:
    446 	case  PT_DETACH:
    447 	case  PT_LWPINFO:
    448 	case  PT_SYSCALL:
    449 	case  PT_SYSCALLEMU:
    450 	case  PT_DUMPCORE:
    451 #ifdef PT_STEP
    452 	case  PT_STEP:
    453 	case  PT_SETSTEP:
    454 	case  PT_CLEARSTEP:
    455 #endif
    456 	case  PT_SET_EVENT_MASK:
    457 	case  PT_GET_EVENT_MASK:
    458 	case  PT_GET_PROCESS_STATE:
    459 	case  PT_RESUME:
    460 	case  PT_SUSPEND:
    461 		/*
    462 		 * You can't do what you want to the process if:
    463 		 *	(1) It's not being traced at all,
    464 		 */
    465 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
    466 			error = EPERM;
    467 			break;
    468 		}
    469 
    470 		/*
    471 		 *	(2) it's being traced by procfs (which has
    472 		 *	    different signal delivery semantics),
    473 		 */
    474 		if (ISSET(t->p_slflag, PSL_FSTRACE)) {
    475 			DPRINTF(("file system traced\n"));
    476 			error = EBUSY;
    477 			break;
    478 		}
    479 
    480 		/*
    481 		 *	(3) 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 		 *	(4) 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_FSTRACE|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_RESUME:
   1120 		write = 1;
   1121 
   1122 	case  PT_SUSPEND:
   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 		if (lt->l_flag & LW_SYSTEM) {
   1139 			error = EINVAL;
   1140 		} else {
   1141 			lwp_lock(lt);
   1142 			if (write == 0)
   1143 				lt->l_flag |= LW_WSUSPEND;
   1144 			else
   1145 				lt->l_flag &= ~LW_WSUSPEND;
   1146 			lwp_unlock(lt);
   1147 		}
   1148 		break;
   1149 
   1150 #ifdef PT_SETREGS
   1151 	case  PT_SETREGS:
   1152 		write = 1;
   1153 #endif
   1154 #ifdef PT_GETREGS
   1155 	case  PT_GETREGS:
   1156 		/* write = 0 done above. */
   1157 #endif
   1158 #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1159 		tmp = data;
   1160 		if (tmp != 0 && t->p_nlwps > 1) {
   1161 			lwp_delref(lt);
   1162 			mutex_enter(t->p_lock);
   1163 			lt = lwp_find(t, tmp);
   1164 			if (lt == NULL) {
   1165 				mutex_exit(t->p_lock);
   1166 				error = ESRCH;
   1167 				break;
   1168 			}
   1169 			lwp_addref(lt);
   1170 			mutex_exit(t->p_lock);
   1171 		}
   1172 		if (!process_validregs(lt))
   1173 			error = EINVAL;
   1174 		else {
   1175 			error = proc_vmspace_getref(p, &vm);
   1176 			if (error)
   1177 				break;
   1178 			iov.iov_base = addr;
   1179 			iov.iov_len = PROC_REGSZ(p);
   1180 			uio.uio_iov = &iov;
   1181 			uio.uio_iovcnt = 1;
   1182 			uio.uio_offset = 0;
   1183 			uio.uio_resid = iov.iov_len;
   1184 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1185 			uio.uio_vmspace = vm;
   1186 
   1187 			error = ptm->ptm_doregs(l, lt, &uio);
   1188 			uvmspace_free(vm);
   1189 		}
   1190 		break;
   1191 #endif
   1192 
   1193 #ifdef PT_SETFPREGS
   1194 	case  PT_SETFPREGS:
   1195 		write = 1;
   1196 		/*FALLTHROUGH*/
   1197 #endif
   1198 #ifdef PT_GETFPREGS
   1199 	case  PT_GETFPREGS:
   1200 		/* write = 0 done above. */
   1201 #endif
   1202 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1203 		tmp = data;
   1204 		if (tmp != 0 && t->p_nlwps > 1) {
   1205 			lwp_delref(lt);
   1206 			mutex_enter(t->p_lock);
   1207 			lt = lwp_find(t, tmp);
   1208 			if (lt == NULL) {
   1209 				mutex_exit(t->p_lock);
   1210 				error = ESRCH;
   1211 				break;
   1212 			}
   1213 			lwp_addref(lt);
   1214 			mutex_exit(t->p_lock);
   1215 		}
   1216 		if (!process_validfpregs(lt))
   1217 			error = EINVAL;
   1218 		else {
   1219 			error = proc_vmspace_getref(p, &vm);
   1220 			if (error)
   1221 				break;
   1222 			iov.iov_base = addr;
   1223 			iov.iov_len = PROC_FPREGSZ(p);
   1224 			uio.uio_iov = &iov;
   1225 			uio.uio_iovcnt = 1;
   1226 			uio.uio_offset = 0;
   1227 			uio.uio_resid = iov.iov_len;
   1228 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1229 			uio.uio_vmspace = vm;
   1230 
   1231 			error = ptm->ptm_dofpregs(l, lt, &uio);
   1232 			uvmspace_free(vm);
   1233 		}
   1234 		break;
   1235 #endif
   1236 
   1237 #ifdef PT_SETDBREGS
   1238 	case  PT_SETDBREGS:
   1239 		write = 1;
   1240 		/*FALLTHROUGH*/
   1241 #endif
   1242 #ifdef PT_GETDBREGS
   1243 	case  PT_GETDBREGS:
   1244 		/* write = 0 done above. */
   1245 #endif
   1246 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1247 		tmp = data;
   1248 		if (tmp != 0 && t->p_nlwps > 1) {
   1249 			lwp_delref(lt);
   1250 			mutex_enter(t->p_lock);
   1251 			lt = lwp_find(t, tmp);
   1252 			if (lt == NULL) {
   1253 				mutex_exit(t->p_lock);
   1254 				error = ESRCH;
   1255 				break;
   1256 			}
   1257 			lwp_addref(lt);
   1258 			mutex_exit(t->p_lock);
   1259 		}
   1260 		if (!process_validdbregs(lt))
   1261 			error = EINVAL;
   1262 		else {
   1263 			error = proc_vmspace_getref(p, &vm);
   1264 			if (error)
   1265 				break;
   1266 			iov.iov_base = addr;
   1267 			iov.iov_len = PROC_DBREGSZ(p);
   1268 			uio.uio_iov = &iov;
   1269 			uio.uio_iovcnt = 1;
   1270 			uio.uio_offset = 0;
   1271 			uio.uio_resid = iov.iov_len;
   1272 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1273 			uio.uio_vmspace = vm;
   1274 
   1275 			error = ptm->ptm_dodbregs(l, lt, &uio);
   1276 			uvmspace_free(vm);
   1277 		}
   1278 		break;
   1279 #endif
   1280 
   1281 #ifdef __HAVE_PTRACE_MACHDEP
   1282 	PTRACE_MACHDEP_REQUEST_CASES
   1283 		error = ptrace_machdep_dorequest(l, lt, req, addr, data);
   1284 		break;
   1285 #endif
   1286 	}
   1287 
   1288 	if (pheld) {
   1289 		mutex_exit(t->p_lock);
   1290 		mutex_exit(proc_lock);
   1291 	}
   1292 	if (lt != NULL)
   1293 		lwp_delref(lt);
   1294 	rw_exit(&t->p_reflock);
   1295 
   1296 	return error;
   1297 }
   1298 
   1299 int
   1300 process_doregs(struct lwp *curl /*tracer*/,
   1301     struct lwp *l /*traced*/,
   1302     struct uio *uio)
   1303 {
   1304 #if defined(PT_GETREGS) || defined(PT_SETREGS)
   1305 	int error;
   1306 	struct reg r;
   1307 	char *kv;
   1308 	int kl;
   1309 
   1310 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1311 		return EINVAL;
   1312 
   1313 	kl = sizeof(r);
   1314 	kv = (char *)&r;
   1315 
   1316 	kv += uio->uio_offset;
   1317 	kl -= uio->uio_offset;
   1318 	if ((size_t)kl > uio->uio_resid)
   1319 		kl = uio->uio_resid;
   1320 
   1321 	error = process_read_regs(l, &r);
   1322 	if (error == 0)
   1323 		error = uiomove(kv, kl, uio);
   1324 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1325 		if (l->l_stat != LSSTOP)
   1326 			error = EBUSY;
   1327 		else
   1328 			error = process_write_regs(l, &r);
   1329 	}
   1330 
   1331 	uio->uio_offset = 0;
   1332 	return error;
   1333 #else
   1334 	return EINVAL;
   1335 #endif
   1336 }
   1337 
   1338 int
   1339 process_validregs(struct lwp *l)
   1340 {
   1341 
   1342 #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1343 	return (l->l_flag & LW_SYSTEM) == 0;
   1344 #else
   1345 	return 0;
   1346 #endif
   1347 }
   1348 
   1349 int
   1350 process_dofpregs(struct lwp *curl /*tracer*/,
   1351     struct lwp *l /*traced*/,
   1352     struct uio *uio)
   1353 {
   1354 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
   1355 	int error;
   1356 	struct fpreg r;
   1357 	char *kv;
   1358 	size_t kl;
   1359 
   1360 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1361 		return EINVAL;
   1362 
   1363 	kl = sizeof(r);
   1364 	kv = (char *)&r;
   1365 
   1366 	kv += uio->uio_offset;
   1367 	kl -= uio->uio_offset;
   1368 	if (kl > uio->uio_resid)
   1369 		kl = uio->uio_resid;
   1370 
   1371 	error = process_read_fpregs(l, &r, &kl);
   1372 	if (error == 0)
   1373 		error = uiomove(kv, kl, uio);
   1374 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1375 		if (l->l_stat != LSSTOP)
   1376 			error = EBUSY;
   1377 		else
   1378 			error = process_write_fpregs(l, &r, kl);
   1379 	}
   1380 	uio->uio_offset = 0;
   1381 	return error;
   1382 #else
   1383 	return EINVAL;
   1384 #endif
   1385 }
   1386 
   1387 int
   1388 process_validfpregs(struct lwp *l)
   1389 {
   1390 
   1391 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1392 	return (l->l_flag & LW_SYSTEM) == 0;
   1393 #else
   1394 	return 0;
   1395 #endif
   1396 }
   1397 
   1398 int
   1399 process_dodbregs(struct lwp *curl /*tracer*/,
   1400     struct lwp *l /*traced*/,
   1401     struct uio *uio)
   1402 {
   1403 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
   1404 	int error;
   1405 	struct dbreg r;
   1406 	char *kv;
   1407 	size_t kl;
   1408 
   1409 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1410 		return EINVAL;
   1411 
   1412 	kl = sizeof(r);
   1413 	kv = (char *)&r;
   1414 
   1415 	kv += uio->uio_offset;
   1416 	kl -= uio->uio_offset;
   1417 	if (kl > uio->uio_resid)
   1418 		kl = uio->uio_resid;
   1419 
   1420 	error = process_read_dbregs(l, &r, &kl);
   1421 	if (error == 0)
   1422 		error = uiomove(kv, kl, uio);
   1423 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1424 		if (l->l_stat != LSSTOP)
   1425 			error = EBUSY;
   1426 		else
   1427 			error = process_write_dbregs(l, &r, kl);
   1428 	}
   1429 	uio->uio_offset = 0;
   1430 	return error;
   1431 #else
   1432 	return EINVAL;
   1433 #endif
   1434 }
   1435 
   1436 int
   1437 process_validdbregs(struct lwp *l)
   1438 {
   1439 
   1440 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1441 	return (l->l_flag & LW_SYSTEM) == 0;
   1442 #else
   1443 	return 0;
   1444 #endif
   1445 }
   1446 
   1447 static int
   1448 process_auxv_offset(struct proc *p, struct uio *uio)
   1449 {
   1450 	struct ps_strings pss;
   1451 	int error;
   1452 	off_t off = (off_t)p->p_psstrp;
   1453 
   1454 	if ((error = copyin_psstrings(p, &pss)) != 0)
   1455 		return error;
   1456 
   1457 	if (pss.ps_envstr == NULL)
   1458 		return EIO;
   1459 
   1460 	uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
   1461 #ifdef __MACHINE_STACK_GROWS_UP
   1462 	if (uio->uio_offset < off)
   1463 		return EIO;
   1464 #else
   1465 	if (uio->uio_offset > off)
   1466 		return EIO;
   1467 	if ((uio->uio_offset + uio->uio_resid) > off)
   1468 		uio->uio_resid = off - uio->uio_offset;
   1469 #endif
   1470 	return 0;
   1471 }
   1472 #endif /* PTRACE */
   1473 
   1474 MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
   1475 
   1476 static int
   1477 ptrace_common_modcmd(modcmd_t cmd, void *arg)
   1478 {
   1479         int error;
   1480 
   1481         switch (cmd) {
   1482         case MODULE_CMD_INIT:
   1483                 error = ptrace_init();
   1484                 break;
   1485         case MODULE_CMD_FINI:
   1486                 error = ptrace_fini();
   1487                 break;
   1488         default:
   1489 		ptrace_hooks();
   1490                 error = ENOTTY;
   1491                 break;
   1492         }
   1493         return error;
   1494 }
   1495