Home | History | Annotate | Line # | Download | only in kern
sys_ptrace_common.c revision 1.17
      1 /*	$NetBSD: sys_ptrace_common.c,v 1.17 2017/02/23 03:34:22 kamil 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.17 2017/02/23 03:34:22 kamil 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 	case PT_SET_SIGMASK:
    217 	case PT_GET_SIGMASK:
    218 #ifdef __HAVE_PTRACE_MACHDEP
    219 	PTRACE_MACHDEP_REQUEST_CASES
    220 #endif
    221 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    222 		    ISSET(p->p_flag, PK_SUGID)) {
    223 			break;
    224 		}
    225 
    226 		result = KAUTH_RESULT_ALLOW;
    227 
    228 	break;
    229 
    230 #ifdef PT_STEP
    231 	case PT_STEP:
    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 #ifdef PT_STEP
    301 	struct lwp *lt2;
    302 #endif
    303 	struct proc *t;				/* target process */
    304 	struct uio uio;
    305 	struct iovec iov;
    306 	struct ptrace_io_desc piod;
    307 	struct ptrace_event pe;
    308 	struct ptrace_state ps;
    309 	struct ptrace_lwpinfo pl;
    310 	struct ptrace_siginfo psi;
    311 	struct vmspace *vm;
    312 	int error, write, tmp, pheld;
    313 	int signo = 0;
    314 	int resume_all;
    315 	ksiginfo_t ksi;
    316 	char *path;
    317 	int len = 0;
    318 	error = 0;
    319 
    320 	/*
    321 	 * If attaching or detaching, we need to get a write hold on the
    322 	 * proclist lock so that we can re-parent the target process.
    323 	 */
    324 	mutex_enter(proc_lock);
    325 
    326 	/* "A foolish consistency..." XXX */
    327 	if (req == PT_TRACE_ME) {
    328 		t = p;
    329 		mutex_enter(t->p_lock);
    330 	} else {
    331 		/* Find the process we're supposed to be operating on. */
    332 		t = proc_find(pid);
    333 		if (t == NULL) {
    334 			mutex_exit(proc_lock);
    335 			return ESRCH;
    336 		}
    337 
    338 		/* XXX-elad */
    339 		mutex_enter(t->p_lock);
    340 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
    341 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    342 		if (error) {
    343 			mutex_exit(proc_lock);
    344 			mutex_exit(t->p_lock);
    345 			return ESRCH;
    346 		}
    347 	}
    348 
    349 	/*
    350 	 * Grab a reference on the process to prevent it from execing or
    351 	 * exiting.
    352 	 */
    353 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
    354 		mutex_exit(proc_lock);
    355 		mutex_exit(t->p_lock);
    356 		return EBUSY;
    357 	}
    358 
    359 	/* Make sure we can operate on it. */
    360 	switch (req) {
    361 	case  PT_TRACE_ME:
    362 		/* Saying that you're being traced is always legal. */
    363 		break;
    364 
    365 	case  PT_ATTACH:
    366 		/*
    367 		 * You can't attach to a process if:
    368 		 *	(1) it's the process that's doing the attaching,
    369 		 */
    370 		if (t->p_pid == p->p_pid) {
    371 			error = EINVAL;
    372 			break;
    373 		}
    374 
    375 		/*
    376 		 *  (2) it's a system process
    377 		 */
    378 		if (t->p_flag & PK_SYSTEM) {
    379 			error = EPERM;
    380 			break;
    381 		}
    382 
    383 		/*
    384 		 *	(3) it's already being traced, or
    385 		 */
    386 		if (ISSET(t->p_slflag, PSL_TRACED)) {
    387 			error = EBUSY;
    388 			break;
    389 		}
    390 
    391 		/*
    392 		 * 	(4) the tracer is chrooted, and its root directory is
    393 		 * 	    not at or above the root directory of the tracee
    394 		 */
    395 		mutex_exit(t->p_lock);	/* XXXSMP */
    396 		tmp = proc_isunder(t, l);
    397 		mutex_enter(t->p_lock);	/* XXXSMP */
    398 		if (!tmp) {
    399 			error = EPERM;
    400 			break;
    401 		}
    402 		break;
    403 
    404 	case  PT_READ_I:
    405 	case  PT_READ_D:
    406 	case  PT_WRITE_I:
    407 	case  PT_WRITE_D:
    408 	case  PT_IO:
    409 	case  PT_SET_SIGINFO:
    410 	case  PT_GET_SIGINFO:
    411 	case  PT_SET_SIGMASK:
    412 	case  PT_GET_SIGMASK:
    413 #ifdef PT_GETREGS
    414 	case  PT_GETREGS:
    415 #endif
    416 #ifdef PT_SETREGS
    417 	case  PT_SETREGS:
    418 #endif
    419 #ifdef PT_GETFPREGS
    420 	case  PT_GETFPREGS:
    421 #endif
    422 #ifdef PT_SETFPREGS
    423 	case  PT_SETFPREGS:
    424 #endif
    425 #ifdef PT_GETDBREGS
    426 	case  PT_GETDBREGS:
    427 #endif
    428 #ifdef PT_SETDBREGS
    429 	case  PT_SETDBREGS:
    430 #endif
    431 #ifdef __HAVE_PTRACE_MACHDEP
    432 	PTRACE_MACHDEP_REQUEST_CASES
    433 #endif
    434 		/*
    435 		 * You can't read/write the memory or registers of a process
    436 		 * if the tracer is chrooted, and its root directory is not at
    437 		 * or above the root directory of the tracee.
    438 		 */
    439 		mutex_exit(t->p_lock);	/* XXXSMP */
    440 		tmp = proc_isunder(t, l);
    441 		mutex_enter(t->p_lock);	/* XXXSMP */
    442 		if (!tmp) {
    443 			error = EPERM;
    444 			break;
    445 		}
    446 		/*FALLTHROUGH*/
    447 
    448 	case  PT_CONTINUE:
    449 	case  PT_KILL:
    450 	case  PT_DETACH:
    451 	case  PT_LWPINFO:
    452 	case  PT_SYSCALL:
    453 	case  PT_SYSCALLEMU:
    454 	case  PT_DUMPCORE:
    455 #ifdef PT_STEP
    456 	case  PT_STEP:
    457 #endif
    458 	case  PT_SET_EVENT_MASK:
    459 	case  PT_GET_EVENT_MASK:
    460 	case  PT_GET_PROCESS_STATE:
    461 	case  PT_RESUME:
    462 	case  PT_SUSPEND:
    463 		/*
    464 		 * You can't do what you want to the process if:
    465 		 *	(1) It's not being traced at all,
    466 		 */
    467 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
    468 			error = EPERM;
    469 			break;
    470 		}
    471 
    472 		/*
    473 		 *	(2) it's being traced by procfs (which has
    474 		 *	    different signal delivery semantics),
    475 		 */
    476 		if (ISSET(t->p_slflag, PSL_FSTRACE)) {
    477 			DPRINTF(("file system traced\n"));
    478 			error = EBUSY;
    479 			break;
    480 		}
    481 
    482 		/*
    483 		 *	(3) it's not being traced by _you_, or
    484 		 */
    485 		if (t->p_pptr != p) {
    486 			DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
    487 			    p->p_pid));
    488 			error = EBUSY;
    489 			break;
    490 		}
    491 
    492 		/*
    493 		 *	(4) it's not currently stopped.
    494 		 */
    495 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
    496 			DPRINTF(("stat %d flag %d\n", t->p_stat,
    497 			    !t->p_waited));
    498 			error = EBUSY;
    499 			break;
    500 		}
    501 		break;
    502 
    503 	default:			/* It was not a legal request. */
    504 		error = EINVAL;
    505 		break;
    506 	}
    507 
    508 	if (error == 0) {
    509 		error = kauth_authorize_process(l->l_cred,
    510 		    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
    511 		    NULL, NULL);
    512 	}
    513 	if (error == 0) {
    514 		lt = lwp_find_first(t);
    515 		if (lt == NULL)
    516 			error = ESRCH;
    517 	}
    518 
    519 	if (error != 0) {
    520 		mutex_exit(proc_lock);
    521 		mutex_exit(t->p_lock);
    522 		rw_exit(&t->p_reflock);
    523 		return error;
    524 	}
    525 
    526 	/* Do single-step fixup if needed. */
    527 	FIX_SSTEP(t);
    528 	KASSERT(lt != NULL);
    529 	lwp_addref(lt);
    530 
    531 	/*
    532 	 * Which locks do we need held? XXX Ugly.
    533 	 */
    534 	switch (req) {
    535 #ifdef PT_STEP
    536 	case PT_STEP:
    537 #endif
    538 	case PT_CONTINUE:
    539 	case PT_DETACH:
    540 	case PT_KILL:
    541 	case PT_SYSCALL:
    542 	case PT_SYSCALLEMU:
    543 	case PT_ATTACH:
    544 	case PT_TRACE_ME:
    545 		pheld = 1;
    546 		break;
    547 	default:
    548 		mutex_exit(proc_lock);
    549 		mutex_exit(t->p_lock);
    550 		pheld = 0;
    551 		break;
    552 	}
    553 
    554 	/* Now do the operation. */
    555 	write = 0;
    556 	*retval = 0;
    557 	tmp = 0;
    558 	resume_all = 1;
    559 
    560 	switch (req) {
    561 	case  PT_TRACE_ME:
    562 		/* Just set the trace flag. */
    563 		SET(t->p_slflag, PSL_TRACED);
    564 		t->p_opptr = t->p_pptr;
    565 		break;
    566 
    567 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
    568 	case  PT_WRITE_D:
    569 #if defined(__HAVE_RAS)
    570 		/*
    571 		 * Can't write to a RAS
    572 		 */
    573 		if (ras_lookup(t, addr) != (void *)-1) {
    574 			error = EACCES;
    575 			break;
    576 		}
    577 #endif
    578 		write = 1;
    579 		tmp = data;
    580 		/* FALLTHROUGH */
    581 
    582 	case  PT_READ_I:		/* XXX no separate I and D spaces */
    583 	case  PT_READ_D:
    584 		/* write = 0 done above. */
    585 		iov.iov_base = (void *)&tmp;
    586 		iov.iov_len = sizeof(tmp);
    587 		uio.uio_iov = &iov;
    588 		uio.uio_iovcnt = 1;
    589 		uio.uio_offset = (off_t)(unsigned long)addr;
    590 		uio.uio_resid = sizeof(tmp);
    591 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
    592 		UIO_SETUP_SYSSPACE(&uio);
    593 
    594 		error = process_domem(l, lt, &uio);
    595 		if (!write)
    596 			*retval = tmp;
    597 		break;
    598 
    599 	case  PT_IO:
    600 		error = ptm->ptm_copyinpiod(&piod, addr);
    601 		if (error)
    602 			break;
    603 
    604 		iov.iov_base = piod.piod_addr;
    605 		iov.iov_len = piod.piod_len;
    606 		uio.uio_iov = &iov;
    607 		uio.uio_iovcnt = 1;
    608 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
    609 		uio.uio_resid = piod.piod_len;
    610 
    611 		switch (piod.piod_op) {
    612 		case PIOD_READ_D:
    613 		case PIOD_READ_I:
    614 			uio.uio_rw = UIO_READ;
    615 			break;
    616 		case PIOD_WRITE_D:
    617 		case PIOD_WRITE_I:
    618 			/*
    619 			 * Can't write to a RAS
    620 			 */
    621 			if (ras_lookup(t, addr) != (void *)-1) {
    622 				return EACCES;
    623 			}
    624 			uio.uio_rw = UIO_WRITE;
    625 			break;
    626 		case PIOD_READ_AUXV:
    627 			req = PT_READ_D;
    628 			uio.uio_rw = UIO_READ;
    629 			tmp = t->p_execsw->es_arglen;
    630 			if (uio.uio_offset > tmp)
    631 				return EIO;
    632 			if (uio.uio_resid > tmp - uio.uio_offset)
    633 				uio.uio_resid = tmp - uio.uio_offset;
    634 			piod.piod_len = iov.iov_len = uio.uio_resid;
    635 			error = process_auxv_offset(t, &uio);
    636 			break;
    637 		default:
    638 			error = EINVAL;
    639 			break;
    640 		}
    641 		if (error)
    642 			break;
    643 		error = proc_vmspace_getref(l->l_proc, &vm);
    644 		if (error)
    645 			break;
    646 		uio.uio_vmspace = vm;
    647 
    648 		error = process_domem(l, lt, &uio);
    649 		piod.piod_len -= uio.uio_resid;
    650 		(void) ptm->ptm_copyoutpiod(&piod, addr);
    651 
    652 		uvmspace_free(vm);
    653 		break;
    654 
    655 	case  PT_DUMPCORE:
    656 		if ((path = addr) != NULL) {
    657 			char *dst;
    658 			len = data;
    659 
    660 			if (len < 0 || len >= MAXPATHLEN) {
    661 				error = EINVAL;
    662 				break;
    663 			}
    664 			dst = kmem_alloc(len + 1, KM_SLEEP);
    665 			if ((error = copyin(path, dst, len)) != 0) {
    666 				kmem_free(dst, len + 1);
    667 				break;
    668 			}
    669 			path = dst;
    670 			path[len] = '\0';
    671 		}
    672 		error = (*coredump_vec)(lt, path);
    673 		if (path)
    674 			kmem_free(path, len + 1);
    675 		break;
    676 
    677 #ifdef PT_STEP
    678 	case  PT_STEP:
    679 		/*
    680 		 * From the 4.4BSD PRM:
    681 		 * "Execution continues as in request PT_CONTINUE; however
    682 		 * as soon as possible after execution of at least one
    683 		 * instruction, execution stops again. [ ... ]"
    684 		 */
    685 #endif
    686 	case  PT_CONTINUE:
    687 	case  PT_SYSCALL:
    688 	case  PT_DETACH:
    689 		if (req == PT_SYSCALL) {
    690 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
    691 				SET(t->p_slflag, PSL_SYSCALL);
    692 #ifdef __HAVE_SYSCALL_INTERN
    693 				(*t->p_emul->e_syscall_intern)(t);
    694 #endif
    695 			}
    696 		} else {
    697 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
    698 				CLR(t->p_slflag, PSL_SYSCALL);
    699 #ifdef __HAVE_SYSCALL_INTERN
    700 				(*t->p_emul->e_syscall_intern)(t);
    701 #endif
    702 			}
    703 		}
    704 		t->p_trace_enabled = trace_is_enabled(t);
    705 
    706 		/*
    707 		 * Pick up the LWPID, if supplied.  There are two cases:
    708 		 * data < 0 : step or continue single thread, lwp = -data
    709 		 * data > 0 in PT_STEP : step this thread, continue others
    710 		 * For operations other than PT_STEP, data > 0 means
    711 		 * data is the signo to deliver to the process.
    712 		 */
    713 		tmp = data;
    714 		if (tmp >= 0) {
    715 #ifdef PT_STEP
    716 			if (req == PT_STEP)
    717 				signo = 0;
    718 			else
    719 #endif
    720 			{
    721 				signo = tmp;
    722 				tmp = 0;	/* don't search for LWP */
    723 			}
    724 		} else
    725 			tmp = -tmp;
    726 
    727 		if (tmp > 0) {
    728 			if (req == PT_DETACH) {
    729 				error = EINVAL;
    730 				break;
    731 			}
    732 			lwp_delref2 (lt);
    733 			lt = lwp_find(t, tmp);
    734 			if (lt == NULL) {
    735 				error = ESRCH;
    736 				break;
    737 			}
    738 			lwp_addref(lt);
    739 			resume_all = 0;
    740 			signo = 0;
    741 		}
    742 
    743 		/*
    744 		 * From the 4.4BSD PRM:
    745 		 * "The data argument is taken as a signal number and the
    746 		 * child's execution continues at location addr as if it
    747 		 * incurred that signal.  Normally the signal number will
    748 		 * be either 0 to indicate that the signal that caused the
    749 		 * stop should be ignored, or that value fetched out of
    750 		 * the process's image indicating which signal caused
    751 		 * the stop.  If addr is (int *)1 then execution continues
    752 		 * from where it stopped."
    753 		 */
    754 
    755 		/* Check that the data is a valid signal number or zero. */
    756 		if (signo < 0 || signo >= NSIG) {
    757 			error = EINVAL;
    758 			break;
    759 		}
    760 
    761 		/* Prevent process deadlock */
    762 		if (resume_all) {
    763 #ifdef PT_STEP
    764 			if (req == PT_STEP) {
    765 				if (lt->l_flag & LW_WSUSPEND) {
    766 					error = EDEADLK;
    767 					break;
    768 				}
    769 			} else
    770 #endif
    771 			{
    772 				error = EDEADLK;
    773 				LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    774 					if ((lt2->l_flag & LW_WSUSPEND) == 0) {
    775 						error = 0;
    776 						break;
    777 					}
    778 				}
    779 				if (error != 0)
    780 					break;
    781 			}
    782 		} else {
    783 			if (lt->l_flag & LW_WSUSPEND) {
    784 				error = EDEADLK;
    785 				break;
    786 			}
    787 		}
    788 
    789 		/* If the address parameter is not (int *)1, set the pc. */
    790 		if ((int *)addr != (int *)1) {
    791 			error = process_set_pc(lt, addr);
    792 			if (error != 0)
    793 				break;
    794 		}
    795 #ifdef PT_STEP
    796 		/*
    797 		 * Arrange for a single-step, if that's requested and possible.
    798 		 * More precisely, set the single step status as requested for
    799 		 * the requested thread, and clear it for other threads.
    800 		 */
    801 		LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    802 			if (lt != lt2) {
    803 				lwp_lock(lt2);
    804 				process_sstep(lt2, 0);
    805 				lwp_unlock(lt2);
    806 			}
    807 		}
    808 		error = process_sstep(lt, req == PT_STEP);
    809 		if (error)
    810 			break;
    811 #endif
    812 		if (req == PT_DETACH) {
    813 			CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
    814 
    815 			/* give process back to original parent or init */
    816 			if (t->p_opptr != t->p_pptr) {
    817 				struct proc *pp = t->p_opptr;
    818 				proc_reparent(t, pp ? pp : initproc);
    819 			}
    820 
    821 			/* not being traced any more */
    822 			t->p_opptr = NULL;
    823 		}
    824 	sendsig:
    825 		t->p_fpid = 0;
    826 		t->p_vfpid = 0;
    827 		t->p_vfpid_done = 0;
    828 		t->p_lwp_created = 0;
    829 		t->p_lwp_exited = 0;
    830 		/* Finally, deliver the requested signal (or none). */
    831 		if (t->p_stat == SSTOP) {
    832 			/*
    833 			 * Unstop the process.  If it needs to take a
    834 			 * signal, make all efforts to ensure that at
    835 			 * an LWP runs to see it.
    836 			 */
    837 			t->p_xsig = signo;
    838 			if (resume_all)
    839 				proc_unstop(t);
    840 			else
    841 				lwp_unstop(lt);
    842 		} else if (t->p_sigctx.ps_faked) {
    843 			if (signo != t->p_sigctx.ps_info._signo) {
    844 				error = EINVAL;
    845 				break;
    846 			}
    847 			t->p_sigctx.ps_faked = false;
    848 			KSI_INIT_EMPTY(&ksi);
    849 			ksi.ksi_info = t->p_sigctx.ps_info;
    850 			ksi.ksi_lid = t->p_sigctx.ps_lwp;
    851 			kpsignal2(t, &ksi);
    852 		} else if (signo != 0) {
    853 			KSI_INIT_EMPTY(&ksi);
    854 			ksi.ksi_signo = signo;
    855 			kpsignal2(t, &ksi);
    856 		}
    857 		break;
    858 
    859 	case  PT_SYSCALLEMU:
    860 		if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
    861 			error = EINVAL;
    862 			break;
    863 		}
    864 		SET(t->p_slflag, PSL_SYSCALLEMU);
    865 		break;
    866 
    867 	case  PT_KILL:
    868 		/* just send the process a KILL signal. */
    869 		signo = SIGKILL;
    870 		goto sendsig;	/* in PT_CONTINUE, above. */
    871 
    872 	case  PT_ATTACH:
    873 		/*
    874 		 * Go ahead and set the trace flag.
    875 		 * Save the old parent (it's reset in
    876 		 *   _DETACH, and also in kern_exit.c:wait4()
    877 		 * Reparent the process so that the tracing
    878 		 *   proc gets to see all the action.
    879 		 * Stop the target.
    880 		 */
    881 		proc_changeparent(t, p);
    882 		signo = SIGSTOP;
    883 		goto sendsig;
    884 
    885 	case  PT_GET_EVENT_MASK:
    886 		if (data != sizeof(pe)) {
    887 			DPRINTF(("ptrace(%d): %d != %zu\n", req,
    888 			    data, sizeof(pe)));
    889 			error = EINVAL;
    890 			break;
    891 		}
    892 		memset(&pe, 0, sizeof(pe));
    893 		pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
    894 		    PTRACE_FORK : 0;
    895 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
    896 		    PTRACE_VFORK : 0;
    897 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
    898 		    PTRACE_VFORK_DONE : 0;
    899 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
    900 		    PTRACE_LWP_CREATE : 0;
    901 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
    902 		    PTRACE_LWP_EXIT : 0;
    903 		error = copyout(&pe, addr, sizeof(pe));
    904 		break;
    905 
    906 	case  PT_SET_EVENT_MASK:
    907 		if (data != sizeof(pe)) {
    908 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    909 			    sizeof(pe)));
    910 			error = EINVAL;
    911 			break;
    912 		}
    913 		if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
    914 			return error;
    915 		if (pe.pe_set_event & PTRACE_FORK)
    916 			SET(t->p_slflag, PSL_TRACEFORK);
    917 		else
    918 			CLR(t->p_slflag, PSL_TRACEFORK);
    919 #if notyet
    920 		if (pe.pe_set_event & PTRACE_VFORK)
    921 			SET(t->p_slflag, PSL_TRACEVFORK);
    922 		else
    923 			CLR(t->p_slflag, PSL_TRACEVFORK);
    924 #else
    925 		if (pe.pe_set_event & PTRACE_VFORK) {
    926 			error = ENOTSUP;
    927 			break;
    928 		}
    929 #endif
    930 		if (pe.pe_set_event & PTRACE_VFORK_DONE)
    931 			SET(t->p_slflag, PSL_TRACEVFORK_DONE);
    932 		else
    933 			CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
    934 		if (pe.pe_set_event & PTRACE_LWP_CREATE)
    935 			SET(t->p_slflag, PSL_TRACELWP_CREATE);
    936 		else
    937 			CLR(t->p_slflag, PSL_TRACELWP_CREATE);
    938 		if (pe.pe_set_event & PTRACE_LWP_EXIT)
    939 			SET(t->p_slflag, PSL_TRACELWP_EXIT);
    940 		else
    941 			CLR(t->p_slflag, PSL_TRACELWP_EXIT);
    942 		break;
    943 
    944 	case  PT_GET_PROCESS_STATE:
    945 		if (data != sizeof(ps)) {
    946 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    947 			    sizeof(ps)));
    948 			error = EINVAL;
    949 			break;
    950 		}
    951 		memset(&ps, 0, sizeof(ps));
    952 		if (t->p_fpid) {
    953 			ps.pe_report_event = PTRACE_FORK;
    954 			ps.pe_other_pid = t->p_fpid;
    955 		} else if (t->p_vfpid) {
    956 			ps.pe_report_event = PTRACE_VFORK;
    957 			ps.pe_other_pid = t->p_vfpid;
    958 		} else if (t->p_vfpid_done) {
    959 			ps.pe_report_event = PTRACE_VFORK_DONE;
    960 			ps.pe_other_pid = t->p_vfpid_done;
    961 		} else if (t->p_lwp_created) {
    962 			ps.pe_report_event = PTRACE_LWP_CREATE;
    963 			ps.pe_lwp = t->p_lwp_created;
    964 		} else if (t->p_lwp_exited) {
    965 			ps.pe_report_event = PTRACE_LWP_EXIT;
    966 			ps.pe_lwp = t->p_lwp_exited;
    967 		}
    968 		error = copyout(&ps, addr, sizeof(ps));
    969 		break;
    970 
    971 	case PT_LWPINFO:
    972 		if (data != sizeof(pl)) {
    973 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    974 			    sizeof(pl)));
    975 			error = EINVAL;
    976 			break;
    977 		}
    978 		error = copyin(addr, &pl, sizeof(pl));
    979 		if (error)
    980 			break;
    981 		tmp = pl.pl_lwpid;
    982 		lwp_delref(lt);
    983 		mutex_enter(t->p_lock);
    984 		if (tmp == 0)
    985 			lt = lwp_find_first(t);
    986 		else {
    987 			lt = lwp_find(t, tmp);
    988 			if (lt == NULL) {
    989 				mutex_exit(t->p_lock);
    990 				error = ESRCH;
    991 				break;
    992 			}
    993 			lt = LIST_NEXT(lt, l_sibling);
    994 		}
    995 		while (lt != NULL && !lwp_alive(lt))
    996 			lt = LIST_NEXT(lt, l_sibling);
    997 		pl.pl_lwpid = 0;
    998 		pl.pl_event = 0;
    999 		if (lt) {
   1000 			lwp_addref(lt);
   1001 			pl.pl_lwpid = lt->l_lid;
   1002 
   1003 			if (lt->l_flag & LW_WSUSPEND)
   1004 				pl.pl_event = PL_EVENT_SUSPENDED;
   1005 			/*
   1006 			 * If we match the lwp, or it was sent to every lwp,
   1007 			 * we set PL_EVENT_SIGNAL.
   1008 			 * XXX: ps_lwp == 0 means everyone and noone, so
   1009 			 * check ps_signo too.
   1010 			 */
   1011 			else if (lt->l_lid == t->p_sigctx.ps_lwp
   1012 			         || (t->p_sigctx.ps_lwp == 0 &&
   1013 			             t->p_sigctx.ps_info._signo))
   1014 				pl.pl_event = PL_EVENT_SIGNAL;
   1015 		}
   1016 		mutex_exit(t->p_lock);
   1017 
   1018 		error = copyout(&pl, addr, sizeof(pl));
   1019 		break;
   1020 
   1021 	case  PT_SET_SIGINFO:
   1022 		if (data != sizeof(psi)) {
   1023 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1024 			    sizeof(psi)));
   1025 			error = EINVAL;
   1026 			break;
   1027 		}
   1028 
   1029 		error = copyin(addr, &psi, sizeof(psi));
   1030 		if (error)
   1031 			break;
   1032 
   1033 		/* Check that the data is a valid signal number or zero. */
   1034 		if (psi.psi_siginfo.si_signo < 0 ||
   1035 		    psi.psi_siginfo.si_signo >= NSIG) {
   1036 			error = EINVAL;
   1037 			break;
   1038 		}
   1039 
   1040 		tmp = psi.psi_lwpid;
   1041 		if (tmp != 0)
   1042 			lwp_delref(lt);
   1043 
   1044 		mutex_enter(t->p_lock);
   1045 
   1046 		if (tmp != 0) {
   1047 			lt = lwp_find(t, tmp);
   1048 			if (lt == NULL) {
   1049 				mutex_exit(t->p_lock);
   1050 				error = ESRCH;
   1051 				break;
   1052 			}
   1053 			lwp_addref(lt);
   1054 		}
   1055 
   1056 		t->p_sigctx.ps_faked = true;
   1057 		t->p_sigctx.ps_info = psi.psi_siginfo._info;
   1058 		t->p_sigctx.ps_lwp = psi.psi_lwpid;
   1059 		mutex_exit(t->p_lock);
   1060 		break;
   1061 
   1062 	case  PT_GET_SIGINFO:
   1063 		if (data != sizeof(psi)) {
   1064 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1065 			    sizeof(psi)));
   1066 			error = EINVAL;
   1067 			break;
   1068 		}
   1069 		mutex_enter(t->p_lock);
   1070 		psi.psi_siginfo._info = t->p_sigctx.ps_info;
   1071 		psi.psi_lwpid = t->p_sigctx.ps_lwp;
   1072 		mutex_exit(t->p_lock);
   1073 
   1074 		error = copyout(&psi, addr, sizeof(psi));
   1075 		if (error)
   1076 			break;
   1077 
   1078 		break;
   1079 
   1080 	case  PT_SET_SIGMASK:
   1081 		write = 1;
   1082 
   1083 	case  PT_GET_SIGMASK:
   1084 		/* write = 0 done above. */
   1085 
   1086 		tmp = data;
   1087 		if (tmp != 0 && t->p_nlwps > 1) {
   1088 			lwp_delref(lt);
   1089 			mutex_enter(t->p_lock);
   1090 			lt = lwp_find(t, tmp);
   1091 			if (lt == NULL) {
   1092 				mutex_exit(t->p_lock);
   1093 				error = ESRCH;
   1094 				break;
   1095 			}
   1096 			lwp_addref(lt);
   1097 			mutex_exit(t->p_lock);
   1098 		}
   1099 
   1100 		if (lt->l_flag & LW_SYSTEM)
   1101 			error = EINVAL;
   1102 		else if (write == 1) {
   1103 			error = copyin(addr, &lt->l_sigmask, sizeof(sigset_t));
   1104 			sigminusset(&sigcantmask, &lt->l_sigmask);
   1105 		} else
   1106 			error = copyout(&lt->l_sigmask, addr, sizeof(sigset_t));
   1107 
   1108 		break;
   1109 
   1110 	case  PT_RESUME:
   1111 		write = 1;
   1112 
   1113 	case  PT_SUSPEND:
   1114 		/* write = 0 done above. */
   1115 
   1116 		tmp = data;
   1117 		if (tmp != 0 && t->p_nlwps > 1) {
   1118 			lwp_delref(lt);
   1119 			mutex_enter(t->p_lock);
   1120 			lt = lwp_find(t, tmp);
   1121 			if (lt == NULL) {
   1122 				mutex_exit(t->p_lock);
   1123 				error = ESRCH;
   1124 				break;
   1125 			}
   1126 			lwp_addref(lt);
   1127 			mutex_exit(t->p_lock);
   1128 		}
   1129 		if (lt->l_flag & LW_SYSTEM) {
   1130 			error = EINVAL;
   1131 		} else {
   1132 			lwp_lock(lt);
   1133 			if (write == 0)
   1134 				lt->l_flag |= LW_WSUSPEND;
   1135 			else
   1136 				lt->l_flag &= ~LW_WSUSPEND;
   1137 			lwp_unlock(lt);
   1138 		}
   1139 		break;
   1140 
   1141 #ifdef PT_SETREGS
   1142 	case  PT_SETREGS:
   1143 		write = 1;
   1144 #endif
   1145 #ifdef PT_GETREGS
   1146 	case  PT_GETREGS:
   1147 		/* write = 0 done above. */
   1148 #endif
   1149 #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1150 		tmp = data;
   1151 		if (tmp != 0 && t->p_nlwps > 1) {
   1152 			lwp_delref(lt);
   1153 			mutex_enter(t->p_lock);
   1154 			lt = lwp_find(t, tmp);
   1155 			if (lt == NULL) {
   1156 				mutex_exit(t->p_lock);
   1157 				error = ESRCH;
   1158 				break;
   1159 			}
   1160 			lwp_addref(lt);
   1161 			mutex_exit(t->p_lock);
   1162 		}
   1163 		if (!process_validregs(lt))
   1164 			error = EINVAL;
   1165 		else {
   1166 			error = proc_vmspace_getref(p, &vm);
   1167 			if (error)
   1168 				break;
   1169 			iov.iov_base = addr;
   1170 			iov.iov_len = PROC_REGSZ(p);
   1171 			uio.uio_iov = &iov;
   1172 			uio.uio_iovcnt = 1;
   1173 			uio.uio_offset = 0;
   1174 			uio.uio_resid = iov.iov_len;
   1175 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1176 			uio.uio_vmspace = vm;
   1177 
   1178 			error = ptm->ptm_doregs(l, lt, &uio);
   1179 			uvmspace_free(vm);
   1180 		}
   1181 		break;
   1182 #endif
   1183 
   1184 #ifdef PT_SETFPREGS
   1185 	case  PT_SETFPREGS:
   1186 		write = 1;
   1187 		/*FALLTHROUGH*/
   1188 #endif
   1189 #ifdef PT_GETFPREGS
   1190 	case  PT_GETFPREGS:
   1191 		/* write = 0 done above. */
   1192 #endif
   1193 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1194 		tmp = data;
   1195 		if (tmp != 0 && t->p_nlwps > 1) {
   1196 			lwp_delref(lt);
   1197 			mutex_enter(t->p_lock);
   1198 			lt = lwp_find(t, tmp);
   1199 			if (lt == NULL) {
   1200 				mutex_exit(t->p_lock);
   1201 				error = ESRCH;
   1202 				break;
   1203 			}
   1204 			lwp_addref(lt);
   1205 			mutex_exit(t->p_lock);
   1206 		}
   1207 		if (!process_validfpregs(lt))
   1208 			error = EINVAL;
   1209 		else {
   1210 			error = proc_vmspace_getref(p, &vm);
   1211 			if (error)
   1212 				break;
   1213 			iov.iov_base = addr;
   1214 			iov.iov_len = PROC_FPREGSZ(p);
   1215 			uio.uio_iov = &iov;
   1216 			uio.uio_iovcnt = 1;
   1217 			uio.uio_offset = 0;
   1218 			uio.uio_resid = iov.iov_len;
   1219 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1220 			uio.uio_vmspace = vm;
   1221 
   1222 			error = ptm->ptm_dofpregs(l, lt, &uio);
   1223 			uvmspace_free(vm);
   1224 		}
   1225 		break;
   1226 #endif
   1227 
   1228 #ifdef PT_SETDBREGS
   1229 	case  PT_SETDBREGS:
   1230 		write = 1;
   1231 		/*FALLTHROUGH*/
   1232 #endif
   1233 #ifdef PT_GETDBREGS
   1234 	case  PT_GETDBREGS:
   1235 		/* write = 0 done above. */
   1236 #endif
   1237 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1238 		tmp = data;
   1239 		if (tmp != 0 && t->p_nlwps > 1) {
   1240 			lwp_delref(lt);
   1241 			mutex_enter(t->p_lock);
   1242 			lt = lwp_find(t, tmp);
   1243 			if (lt == NULL) {
   1244 				mutex_exit(t->p_lock);
   1245 				error = ESRCH;
   1246 				break;
   1247 			}
   1248 			lwp_addref(lt);
   1249 			mutex_exit(t->p_lock);
   1250 		}
   1251 		if (!process_validdbregs(lt))
   1252 			error = EINVAL;
   1253 		else {
   1254 			error = proc_vmspace_getref(p, &vm);
   1255 			if (error)
   1256 				break;
   1257 			iov.iov_base = addr;
   1258 			iov.iov_len = PROC_DBREGSZ(p);
   1259 			uio.uio_iov = &iov;
   1260 			uio.uio_iovcnt = 1;
   1261 			uio.uio_offset = 0;
   1262 			uio.uio_resid = iov.iov_len;
   1263 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1264 			uio.uio_vmspace = vm;
   1265 
   1266 			error = ptm->ptm_dodbregs(l, lt, &uio);
   1267 			uvmspace_free(vm);
   1268 		}
   1269 		break;
   1270 #endif
   1271 
   1272 #ifdef __HAVE_PTRACE_MACHDEP
   1273 	PTRACE_MACHDEP_REQUEST_CASES
   1274 		error = ptrace_machdep_dorequest(l, lt, req, addr, data);
   1275 		break;
   1276 #endif
   1277 	}
   1278 
   1279 	if (pheld) {
   1280 		mutex_exit(t->p_lock);
   1281 		mutex_exit(proc_lock);
   1282 	}
   1283 	if (lt != NULL)
   1284 		lwp_delref(lt);
   1285 	rw_exit(&t->p_reflock);
   1286 
   1287 	return error;
   1288 }
   1289 
   1290 int
   1291 process_doregs(struct lwp *curl /*tracer*/,
   1292     struct lwp *l /*traced*/,
   1293     struct uio *uio)
   1294 {
   1295 #if defined(PT_GETREGS) || defined(PT_SETREGS)
   1296 	int error;
   1297 	struct reg r;
   1298 	char *kv;
   1299 	int kl;
   1300 
   1301 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1302 		return EINVAL;
   1303 
   1304 	kl = sizeof(r);
   1305 	kv = (char *)&r;
   1306 
   1307 	kv += uio->uio_offset;
   1308 	kl -= uio->uio_offset;
   1309 	if ((size_t)kl > uio->uio_resid)
   1310 		kl = uio->uio_resid;
   1311 
   1312 	error = process_read_regs(l, &r);
   1313 	if (error == 0)
   1314 		error = uiomove(kv, kl, uio);
   1315 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1316 		if (l->l_stat != LSSTOP)
   1317 			error = EBUSY;
   1318 		else
   1319 			error = process_write_regs(l, &r);
   1320 	}
   1321 
   1322 	uio->uio_offset = 0;
   1323 	return error;
   1324 #else
   1325 	return EINVAL;
   1326 #endif
   1327 }
   1328 
   1329 int
   1330 process_validregs(struct lwp *l)
   1331 {
   1332 
   1333 #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1334 	return (l->l_flag & LW_SYSTEM) == 0;
   1335 #else
   1336 	return 0;
   1337 #endif
   1338 }
   1339 
   1340 int
   1341 process_dofpregs(struct lwp *curl /*tracer*/,
   1342     struct lwp *l /*traced*/,
   1343     struct uio *uio)
   1344 {
   1345 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
   1346 	int error;
   1347 	struct fpreg r;
   1348 	char *kv;
   1349 	size_t kl;
   1350 
   1351 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1352 		return EINVAL;
   1353 
   1354 	kl = sizeof(r);
   1355 	kv = (char *)&r;
   1356 
   1357 	kv += uio->uio_offset;
   1358 	kl -= uio->uio_offset;
   1359 	if (kl > uio->uio_resid)
   1360 		kl = uio->uio_resid;
   1361 
   1362 	error = process_read_fpregs(l, &r, &kl);
   1363 	if (error == 0)
   1364 		error = uiomove(kv, kl, uio);
   1365 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1366 		if (l->l_stat != LSSTOP)
   1367 			error = EBUSY;
   1368 		else
   1369 			error = process_write_fpregs(l, &r, kl);
   1370 	}
   1371 	uio->uio_offset = 0;
   1372 	return error;
   1373 #else
   1374 	return EINVAL;
   1375 #endif
   1376 }
   1377 
   1378 int
   1379 process_validfpregs(struct lwp *l)
   1380 {
   1381 
   1382 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1383 	return (l->l_flag & LW_SYSTEM) == 0;
   1384 #else
   1385 	return 0;
   1386 #endif
   1387 }
   1388 
   1389 int
   1390 process_dodbregs(struct lwp *curl /*tracer*/,
   1391     struct lwp *l /*traced*/,
   1392     struct uio *uio)
   1393 {
   1394 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
   1395 	int error;
   1396 	struct dbreg r;
   1397 	char *kv;
   1398 	size_t kl;
   1399 
   1400 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1401 		return EINVAL;
   1402 
   1403 	kl = sizeof(r);
   1404 	kv = (char *)&r;
   1405 
   1406 	kv += uio->uio_offset;
   1407 	kl -= uio->uio_offset;
   1408 	if (kl > uio->uio_resid)
   1409 		kl = uio->uio_resid;
   1410 
   1411 	error = process_read_dbregs(l, &r, &kl);
   1412 	if (error == 0)
   1413 		error = uiomove(kv, kl, uio);
   1414 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1415 		if (l->l_stat != LSSTOP)
   1416 			error = EBUSY;
   1417 		else
   1418 			error = process_write_dbregs(l, &r, kl);
   1419 	}
   1420 	uio->uio_offset = 0;
   1421 	return error;
   1422 #else
   1423 	return EINVAL;
   1424 #endif
   1425 }
   1426 
   1427 int
   1428 process_validdbregs(struct lwp *l)
   1429 {
   1430 
   1431 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1432 	return (l->l_flag & LW_SYSTEM) == 0;
   1433 #else
   1434 	return 0;
   1435 #endif
   1436 }
   1437 
   1438 static int
   1439 process_auxv_offset(struct proc *p, struct uio *uio)
   1440 {
   1441 	struct ps_strings pss;
   1442 	int error;
   1443 	off_t off = (off_t)p->p_psstrp;
   1444 
   1445 	if ((error = copyin_psstrings(p, &pss)) != 0)
   1446 		return error;
   1447 
   1448 	if (pss.ps_envstr == NULL)
   1449 		return EIO;
   1450 
   1451 	uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
   1452 #ifdef __MACHINE_STACK_GROWS_UP
   1453 	if (uio->uio_offset < off)
   1454 		return EIO;
   1455 #else
   1456 	if (uio->uio_offset > off)
   1457 		return EIO;
   1458 	if ((uio->uio_offset + uio->uio_resid) > off)
   1459 		uio->uio_resid = off - uio->uio_offset;
   1460 #endif
   1461 	return 0;
   1462 }
   1463 #endif /* PTRACE */
   1464 
   1465 MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
   1466 
   1467 static int
   1468 ptrace_common_modcmd(modcmd_t cmd, void *arg)
   1469 {
   1470         int error;
   1471 
   1472         switch (cmd) {
   1473         case MODULE_CMD_INIT:
   1474                 error = ptrace_init();
   1475                 break;
   1476         case MODULE_CMD_FINI:
   1477                 error = ptrace_fini();
   1478                 break;
   1479         default:
   1480 		ptrace_hooks();
   1481                 error = ENOTTY;
   1482                 break;
   1483         }
   1484         return error;
   1485 }
   1486