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