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