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