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