xref: /freebsd/sys/kern/sys_process.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1994, Sean Eric Fagan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Sean Eric Fagan.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/ktr.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/reg.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysent.h>
44 #include <sys/sysproto.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/ptrace.h>
49 #include <sys/rwlock.h>
50 #include <sys/sx.h>
51 #include <sys/malloc.h>
52 #include <sys/signalvar.h>
53 #include <sys/caprights.h>
54 #include <sys/filedesc.h>
55 
56 #include <security/audit/audit.h>
57 
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_param.h>
66 
67 #ifdef COMPAT_FREEBSD32
68 #include <sys/procfs.h>
69 #endif
70 
71 /* Assert it's safe to unlock a process, e.g. to allocate working memory */
72 #define	PROC_ASSERT_TRACEREQ(p)	MPASS(((p)->p_flag2 & P2_PTRACEREQ) != 0)
73 
74 /*
75  * Functions implemented using PROC_ACTION():
76  *
77  * proc_read_regs(proc, regs)
78  *	Get the current user-visible register set from the process
79  *	and copy it into the regs structure (<machine/reg.h>).
80  *	The process is stopped at the time read_regs is called.
81  *
82  * proc_write_regs(proc, regs)
83  *	Update the current register set from the passed in regs
84  *	structure.  Take care to avoid clobbering special CPU
85  *	registers or privileged bits in the PSL.
86  *	Depending on the architecture this may have fix-up work to do,
87  *	especially if the IAR or PCW are modified.
88  *	The process is stopped at the time write_regs is called.
89  *
90  * proc_read_fpregs, proc_write_fpregs
91  *	deal with the floating point register set, otherwise as above.
92  *
93  * proc_read_dbregs, proc_write_dbregs
94  *	deal with the processor debug register set, otherwise as above.
95  *
96  * proc_sstep(proc)
97  *	Arrange for the process to trap after executing a single instruction.
98  */
99 
100 #define	PROC_ACTION(action) do {					\
101 	int error;							\
102 									\
103 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);			\
104 	if ((td->td_proc->p_flag & P_INMEM) == 0)			\
105 		error = EIO;						\
106 	else								\
107 		error = (action);					\
108 	return (error);							\
109 } while (0)
110 
111 int
112 proc_read_regs(struct thread *td, struct reg *regs)
113 {
114 
115 	PROC_ACTION(fill_regs(td, regs));
116 }
117 
118 int
119 proc_write_regs(struct thread *td, struct reg *regs)
120 {
121 
122 	PROC_ACTION(set_regs(td, regs));
123 }
124 
125 int
126 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
127 {
128 
129 	PROC_ACTION(fill_dbregs(td, dbregs));
130 }
131 
132 int
133 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
134 {
135 
136 	PROC_ACTION(set_dbregs(td, dbregs));
137 }
138 
139 /*
140  * Ptrace doesn't support fpregs at all, and there are no security holes
141  * or translations for fpregs, so we can just copy them.
142  */
143 int
144 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
145 {
146 
147 	PROC_ACTION(fill_fpregs(td, fpregs));
148 }
149 
150 int
151 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
152 {
153 
154 	PROC_ACTION(set_fpregs(td, fpregs));
155 }
156 
157 static struct regset *
158 proc_find_regset(struct thread *td, int note)
159 {
160 	struct regset **regsetp, **regset_end, *regset;
161 	struct sysentvec *sv;
162 
163 	sv = td->td_proc->p_sysent;
164 	regsetp = sv->sv_regset_begin;
165 	if (regsetp == NULL)
166 		return (NULL);
167 	regset_end = sv->sv_regset_end;
168 	MPASS(regset_end != NULL);
169 	for (; regsetp < regset_end; regsetp++) {
170 		regset = *regsetp;
171 		if (regset->note != note)
172 			continue;
173 
174 		return (regset);
175 	}
176 
177 	return (NULL);
178 }
179 
180 static int
181 proc_read_regset(struct thread *td, int note, struct iovec *iov)
182 {
183 	struct regset *regset;
184 	struct proc *p;
185 	void *buf;
186 	size_t size;
187 	int error;
188 
189 	regset = proc_find_regset(td, note);
190 	if (regset == NULL)
191 		return (EINVAL);
192 
193 	if (iov->iov_base == NULL) {
194 		iov->iov_len = regset->size;
195 		if (iov->iov_len == 0)
196 			return (EINVAL);
197 
198 		return (0);
199 	}
200 
201 	/* The length is wrong, return an error */
202 	if (iov->iov_len != regset->size)
203 		return (EINVAL);
204 
205 	if (regset->get == NULL)
206 		return (EINVAL);
207 
208 	error = 0;
209 	size = regset->size;
210 	p = td->td_proc;
211 
212 	/* Drop the proc lock while allocating the temp buffer */
213 	PROC_ASSERT_TRACEREQ(p);
214 	PROC_UNLOCK(p);
215 	buf = malloc(size, M_TEMP, M_WAITOK);
216 	PROC_LOCK(p);
217 
218 	if (!regset->get(regset, td, buf, &size)) {
219 		error = EINVAL;
220 	} else {
221 		KASSERT(size == regset->size,
222 		    ("%s: Getter function changed the size", __func__));
223 
224 		iov->iov_len = size;
225 		PROC_UNLOCK(p);
226 		error = copyout(buf, iov->iov_base, size);
227 		PROC_LOCK(p);
228 	}
229 
230 	free(buf, M_TEMP);
231 
232 	return (error);
233 }
234 
235 static int
236 proc_write_regset(struct thread *td, int note, struct iovec *iov)
237 {
238 	struct regset *regset;
239 	struct proc *p;
240 	void *buf;
241 	size_t size;
242 	int error;
243 
244 	regset = proc_find_regset(td, note);
245 	if (regset == NULL)
246 		return (EINVAL);
247 
248 	/* The length is wrong, return an error */
249 	if (iov->iov_len != regset->size)
250 		return (EINVAL);
251 
252 	if (regset->set == NULL)
253 		return (EINVAL);
254 
255 	size = regset->size;
256 	p = td->td_proc;
257 
258 	/* Drop the proc lock while allocating the temp buffer */
259 	PROC_ASSERT_TRACEREQ(p);
260 	PROC_UNLOCK(p);
261 	buf = malloc(size, M_TEMP, M_WAITOK);
262 	error = copyin(iov->iov_base, buf, size);
263 	PROC_LOCK(p);
264 
265 	if (error == 0) {
266 		if (!regset->set(regset, td, buf, size)) {
267 			error = EINVAL;
268 		}
269 	}
270 
271 	free(buf, M_TEMP);
272 
273 	return (error);
274 }
275 
276 #ifdef COMPAT_FREEBSD32
277 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
278 int
279 proc_read_regs32(struct thread *td, struct reg32 *regs32)
280 {
281 
282 	PROC_ACTION(fill_regs32(td, regs32));
283 }
284 
285 int
286 proc_write_regs32(struct thread *td, struct reg32 *regs32)
287 {
288 
289 	PROC_ACTION(set_regs32(td, regs32));
290 }
291 
292 int
293 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
294 {
295 
296 	PROC_ACTION(fill_dbregs32(td, dbregs32));
297 }
298 
299 int
300 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
301 {
302 
303 	PROC_ACTION(set_dbregs32(td, dbregs32));
304 }
305 
306 int
307 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
308 {
309 
310 	PROC_ACTION(fill_fpregs32(td, fpregs32));
311 }
312 
313 int
314 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
315 {
316 
317 	PROC_ACTION(set_fpregs32(td, fpregs32));
318 }
319 #endif
320 
321 int
322 proc_sstep(struct thread *td)
323 {
324 
325 	PROC_ACTION(ptrace_single_step(td));
326 }
327 
328 int
329 proc_rwmem(struct proc *p, struct uio *uio)
330 {
331 	vm_map_t map;
332 	vm_offset_t pageno;		/* page number */
333 	vm_prot_t reqprot;
334 	int error, fault_flags, page_offset, writing;
335 
336 	/*
337 	 * Make sure that the process' vmspace remains live.
338 	 */
339 	if (p != curproc)
340 		PROC_ASSERT_HELD(p);
341 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
342 
343 	/*
344 	 * The map we want...
345 	 */
346 	map = &p->p_vmspace->vm_map;
347 
348 	/*
349 	 * If we are writing, then we request vm_fault() to create a private
350 	 * copy of each page.  Since these copies will not be writeable by the
351 	 * process, we must explicity request that they be dirtied.
352 	 */
353 	writing = uio->uio_rw == UIO_WRITE;
354 	reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
355 	fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
356 
357 	/*
358 	 * Only map in one page at a time.  We don't have to, but it
359 	 * makes things easier.  This way is trivial - right?
360 	 */
361 	do {
362 		vm_offset_t uva;
363 		u_int len;
364 		vm_page_t m;
365 
366 		uva = (vm_offset_t)uio->uio_offset;
367 
368 		/*
369 		 * Get the page number of this segment.
370 		 */
371 		pageno = trunc_page(uva);
372 		page_offset = uva - pageno;
373 
374 		/*
375 		 * How many bytes to copy
376 		 */
377 		len = min(PAGE_SIZE - page_offset, uio->uio_resid);
378 
379 		/*
380 		 * Fault and hold the page on behalf of the process.
381 		 */
382 		error = vm_fault(map, pageno, reqprot, fault_flags, &m);
383 		if (error != KERN_SUCCESS) {
384 			if (error == KERN_RESOURCE_SHORTAGE)
385 				error = ENOMEM;
386 			else
387 				error = EFAULT;
388 			break;
389 		}
390 
391 		/*
392 		 * Now do the i/o move.
393 		 */
394 		error = uiomove_fromphys(&m, page_offset, len, uio);
395 
396 		/* Make the I-cache coherent for breakpoints. */
397 		if (writing && error == 0) {
398 			vm_map_lock_read(map);
399 			if (vm_map_check_protection(map, pageno, pageno +
400 			    PAGE_SIZE, VM_PROT_EXECUTE))
401 				vm_sync_icache(map, uva, len);
402 			vm_map_unlock_read(map);
403 		}
404 
405 		/*
406 		 * Release the page.
407 		 */
408 		vm_page_unwire(m, PQ_ACTIVE);
409 
410 	} while (error == 0 && uio->uio_resid > 0);
411 
412 	return (error);
413 }
414 
415 static ssize_t
416 proc_iop(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
417     size_t len, enum uio_rw rw)
418 {
419 	struct iovec iov;
420 	struct uio uio;
421 	ssize_t slen;
422 
423 	MPASS(len < SSIZE_MAX);
424 	slen = (ssize_t)len;
425 
426 	iov.iov_base = (caddr_t)buf;
427 	iov.iov_len = len;
428 	uio.uio_iov = &iov;
429 	uio.uio_iovcnt = 1;
430 	uio.uio_offset = va;
431 	uio.uio_resid = slen;
432 	uio.uio_segflg = UIO_SYSSPACE;
433 	uio.uio_rw = rw;
434 	uio.uio_td = td;
435 	proc_rwmem(p, &uio);
436 	if (uio.uio_resid == slen)
437 		return (-1);
438 	return (slen - uio.uio_resid);
439 }
440 
441 ssize_t
442 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
443     size_t len)
444 {
445 
446 	return (proc_iop(td, p, va, buf, len, UIO_READ));
447 }
448 
449 ssize_t
450 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
451     size_t len)
452 {
453 
454 	return (proc_iop(td, p, va, buf, len, UIO_WRITE));
455 }
456 
457 static int
458 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
459 {
460 	struct vattr vattr;
461 	vm_map_t map;
462 	vm_map_entry_t entry;
463 	vm_object_t obj, tobj, lobj;
464 	struct vmspace *vm;
465 	struct vnode *vp;
466 	char *freepath, *fullpath;
467 	u_int pathlen;
468 	int error, index;
469 
470 	error = 0;
471 	obj = NULL;
472 
473 	vm = vmspace_acquire_ref(p);
474 	map = &vm->vm_map;
475 	vm_map_lock_read(map);
476 
477 	do {
478 		KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
479 		    ("Submap in map header"));
480 		index = 0;
481 		VM_MAP_ENTRY_FOREACH(entry, map) {
482 			if (index >= pve->pve_entry &&
483 			    (entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
484 				break;
485 			index++;
486 		}
487 		if (index < pve->pve_entry) {
488 			error = EINVAL;
489 			break;
490 		}
491 		if (entry == &map->header) {
492 			error = ENOENT;
493 			break;
494 		}
495 
496 		/* We got an entry. */
497 		pve->pve_entry = index + 1;
498 		pve->pve_timestamp = map->timestamp;
499 		pve->pve_start = entry->start;
500 		pve->pve_end = entry->end - 1;
501 		pve->pve_offset = entry->offset;
502 		pve->pve_prot = entry->protection;
503 
504 		/* Backing object's path needed? */
505 		if (pve->pve_pathlen == 0)
506 			break;
507 
508 		pathlen = pve->pve_pathlen;
509 		pve->pve_pathlen = 0;
510 
511 		obj = entry->object.vm_object;
512 		if (obj != NULL)
513 			VM_OBJECT_RLOCK(obj);
514 	} while (0);
515 
516 	vm_map_unlock_read(map);
517 
518 	pve->pve_fsid = VNOVAL;
519 	pve->pve_fileid = VNOVAL;
520 
521 	if (error == 0 && obj != NULL) {
522 		lobj = obj;
523 		for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
524 			if (tobj != obj)
525 				VM_OBJECT_RLOCK(tobj);
526 			if (lobj != obj)
527 				VM_OBJECT_RUNLOCK(lobj);
528 			lobj = tobj;
529 			pve->pve_offset += tobj->backing_object_offset;
530 		}
531 		vp = vm_object_vnode(lobj);
532 		if (vp != NULL)
533 			vref(vp);
534 		if (lobj != obj)
535 			VM_OBJECT_RUNLOCK(lobj);
536 		VM_OBJECT_RUNLOCK(obj);
537 
538 		if (vp != NULL) {
539 			freepath = NULL;
540 			fullpath = NULL;
541 			vn_fullpath(vp, &fullpath, &freepath);
542 			vn_lock(vp, LK_SHARED | LK_RETRY);
543 			if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
544 				pve->pve_fileid = vattr.va_fileid;
545 				pve->pve_fsid = vattr.va_fsid;
546 			}
547 			vput(vp);
548 
549 			if (fullpath != NULL) {
550 				pve->pve_pathlen = strlen(fullpath) + 1;
551 				if (pve->pve_pathlen <= pathlen) {
552 					error = copyout(fullpath, pve->pve_path,
553 					    pve->pve_pathlen);
554 				} else
555 					error = ENAMETOOLONG;
556 			}
557 			if (freepath != NULL)
558 				free(freepath, M_TEMP);
559 		}
560 	}
561 	vmspace_free(vm);
562 	if (error == 0)
563 		CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p",
564 		    p->p_pid, pve->pve_entry, pve->pve_start);
565 
566 	return (error);
567 }
568 
569 /*
570  * Process debugging system call.
571  */
572 #ifndef _SYS_SYSPROTO_H_
573 struct ptrace_args {
574 	int	req;
575 	pid_t	pid;
576 	caddr_t	addr;
577 	int	data;
578 };
579 #endif
580 
581 int
582 sys_ptrace(struct thread *td, struct ptrace_args *uap)
583 {
584 	/*
585 	 * XXX this obfuscation is to reduce stack usage, but the register
586 	 * structs may be too large to put on the stack anyway.
587 	 */
588 	union {
589 		struct ptrace_io_desc piod;
590 		struct ptrace_lwpinfo pl;
591 		struct ptrace_vm_entry pve;
592 		struct ptrace_coredump pc;
593 		struct ptrace_sc_remote sr;
594 		struct dbreg dbreg;
595 		struct fpreg fpreg;
596 		struct reg reg;
597 		struct iovec vec;
598 		syscallarg_t args[nitems(td->td_sa.args)];
599 		struct ptrace_sc_ret psr;
600 		int ptevents;
601 	} r;
602 	syscallarg_t pscr_args[nitems(td->td_sa.args)];
603 	void *addr;
604 	int error;
605 
606 	if (!allow_ptrace)
607 		return (ENOSYS);
608 	error = 0;
609 
610 	AUDIT_ARG_PID(uap->pid);
611 	AUDIT_ARG_CMD(uap->req);
612 	AUDIT_ARG_VALUE(uap->data);
613 	addr = &r;
614 	switch (uap->req) {
615 	case PT_GET_EVENT_MASK:
616 	case PT_LWPINFO:
617 	case PT_GET_SC_ARGS:
618 	case PT_GET_SC_RET:
619 		break;
620 	case PT_GETREGS:
621 		bzero(&r.reg, sizeof(r.reg));
622 		break;
623 	case PT_GETFPREGS:
624 		bzero(&r.fpreg, sizeof(r.fpreg));
625 		break;
626 	case PT_GETDBREGS:
627 		bzero(&r.dbreg, sizeof(r.dbreg));
628 		break;
629 	case PT_GETREGSET:
630 	case PT_SETREGSET:
631 		error = copyin(uap->addr, &r.vec, sizeof(r.vec));
632 		break;
633 	case PT_SETREGS:
634 		error = copyin(uap->addr, &r.reg, sizeof(r.reg));
635 		break;
636 	case PT_SETFPREGS:
637 		error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
638 		break;
639 	case PT_SETDBREGS:
640 		error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
641 		break;
642 	case PT_SET_EVENT_MASK:
643 		if (uap->data != sizeof(r.ptevents))
644 			error = EINVAL;
645 		else
646 			error = copyin(uap->addr, &r.ptevents, uap->data);
647 		break;
648 	case PT_IO:
649 		error = copyin(uap->addr, &r.piod, sizeof(r.piod));
650 		break;
651 	case PT_VM_ENTRY:
652 		error = copyin(uap->addr, &r.pve, sizeof(r.pve));
653 		break;
654 	case PT_COREDUMP:
655 		if (uap->data != sizeof(r.pc))
656 			error = EINVAL;
657 		else
658 			error = copyin(uap->addr, &r.pc, uap->data);
659 		break;
660 	case PT_SC_REMOTE:
661 		if (uap->data != sizeof(r.sr)) {
662 			error = EINVAL;
663 			break;
664 		}
665 		error = copyin(uap->addr, &r.sr, uap->data);
666 		if (error != 0)
667 			break;
668 		if (r.sr.pscr_nargs > nitems(td->td_sa.args)) {
669 			error = EINVAL;
670 			break;
671 		}
672 		error = copyin(r.sr.pscr_args, pscr_args,
673 		    sizeof(u_long) * r.sr.pscr_nargs);
674 		if (error != 0)
675 			break;
676 		r.sr.pscr_args = pscr_args;
677 		break;
678 	default:
679 		addr = uap->addr;
680 		break;
681 	}
682 	if (error)
683 		return (error);
684 
685 	error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
686 	if (error)
687 		return (error);
688 
689 	switch (uap->req) {
690 	case PT_VM_ENTRY:
691 		error = copyout(&r.pve, uap->addr, sizeof(r.pve));
692 		break;
693 	case PT_IO:
694 		error = copyout(&r.piod, uap->addr, sizeof(r.piod));
695 		break;
696 	case PT_GETREGS:
697 		error = copyout(&r.reg, uap->addr, sizeof(r.reg));
698 		break;
699 	case PT_GETFPREGS:
700 		error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
701 		break;
702 	case PT_GETDBREGS:
703 		error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
704 		break;
705 	case PT_GETREGSET:
706 		error = copyout(&r.vec, uap->addr, sizeof(r.vec));
707 		break;
708 	case PT_GET_EVENT_MASK:
709 		/* NB: The size in uap->data is validated in kern_ptrace(). */
710 		error = copyout(&r.ptevents, uap->addr, uap->data);
711 		break;
712 	case PT_LWPINFO:
713 		/* NB: The size in uap->data is validated in kern_ptrace(). */
714 		error = copyout(&r.pl, uap->addr, uap->data);
715 		break;
716 	case PT_GET_SC_ARGS:
717 		error = copyout(r.args, uap->addr, MIN(uap->data,
718 		    sizeof(r.args)));
719 		break;
720 	case PT_GET_SC_RET:
721 		error = copyout(&r.psr, uap->addr, MIN(uap->data,
722 		    sizeof(r.psr)));
723 		break;
724 	case PT_SC_REMOTE:
725 		error = copyout(&r.sr.pscr_ret, uap->addr +
726 		    offsetof(struct ptrace_sc_remote, pscr_ret),
727 		    sizeof(r.sr.pscr_ret));
728 		break;
729 	}
730 
731 	return (error);
732 }
733 
734 #ifdef COMPAT_FREEBSD32
735 /*
736  *   PROC_READ(regs, td2, addr);
737  * becomes either:
738  *   proc_read_regs(td2, addr);
739  * or
740  *   proc_read_regs32(td2, addr);
741  * .. except this is done at runtime.  There is an additional
742  * complication in that PROC_WRITE disallows 32 bit consumers
743  * from writing to 64 bit address space targets.
744  */
745 #define	PROC_READ(w, t, a)	wrap32 ? \
746 	proc_read_ ## w ## 32(t, a) : \
747 	proc_read_ ## w (t, a)
748 #define	PROC_WRITE(w, t, a)	wrap32 ? \
749 	(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
750 	proc_write_ ## w (t, a)
751 #else
752 #define	PROC_READ(w, t, a)	proc_read_ ## w (t, a)
753 #define	PROC_WRITE(w, t, a)	proc_write_ ## w (t, a)
754 #endif
755 
756 void
757 proc_set_traced(struct proc *p, bool stop)
758 {
759 
760 	sx_assert(&proctree_lock, SX_XLOCKED);
761 	PROC_LOCK_ASSERT(p, MA_OWNED);
762 	p->p_flag |= P_TRACED;
763 	if (stop)
764 		p->p_flag2 |= P2_PTRACE_FSTP;
765 	p->p_ptevents = PTRACE_DEFAULT;
766 }
767 
768 void
769 ptrace_unsuspend(struct proc *p)
770 {
771 	PROC_LOCK_ASSERT(p, MA_OWNED);
772 
773 	PROC_SLOCK(p);
774 	p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
775 	thread_unsuspend(p);
776 	PROC_SUNLOCK(p);
777 	itimer_proc_continue(p);
778 	kqtimer_proc_continue(p);
779 }
780 
781 static int
782 proc_can_ptrace(struct thread *td, struct proc *p)
783 {
784 	int error;
785 
786 	PROC_LOCK_ASSERT(p, MA_OWNED);
787 
788 	if ((p->p_flag & P_WEXIT) != 0)
789 		return (ESRCH);
790 
791 	if ((error = p_cansee(td, p)) != 0)
792 		return (error);
793 	if ((error = p_candebug(td, p)) != 0)
794 		return (error);
795 
796 	/* not being traced... */
797 	if ((p->p_flag & P_TRACED) == 0)
798 		return (EPERM);
799 
800 	/* not being traced by YOU */
801 	if (p->p_pptr != td->td_proc)
802 		return (EBUSY);
803 
804 	/* not currently stopped */
805 	if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
806 	    p->p_suspcount != p->p_numthreads  ||
807 	    (p->p_flag & P_WAITED) == 0)
808 		return (EBUSY);
809 
810 	return (0);
811 }
812 
813 static struct thread *
814 ptrace_sel_coredump_thread(struct proc *p)
815 {
816 	struct thread *td2;
817 
818 	PROC_LOCK_ASSERT(p, MA_OWNED);
819 	MPASS((p->p_flag & P_STOPPED_TRACE) != 0);
820 
821 	FOREACH_THREAD_IN_PROC(p, td2) {
822 		if ((td2->td_dbgflags & TDB_SSWITCH) != 0)
823 			return (td2);
824 	}
825 	return (NULL);
826 }
827 
828 int
829 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
830 {
831 	struct iovec iov;
832 	struct uio uio;
833 	struct proc *curp, *p, *pp;
834 	struct thread *td2 = NULL, *td3;
835 	struct ptrace_io_desc *piod = NULL;
836 	struct ptrace_lwpinfo *pl;
837 	struct ptrace_sc_ret *psr;
838 	struct ptrace_sc_remote *pscr;
839 	struct file *fp;
840 	struct ptrace_coredump *pc;
841 	struct thr_coredump_req *tcq;
842 	struct thr_syscall_req *tsr;
843 	int error, num, tmp;
844 	lwpid_t tid = 0, *buf;
845 #ifdef COMPAT_FREEBSD32
846 	int wrap32 = 0, safe = 0;
847 #endif
848 	bool proctree_locked, p2_req_set;
849 
850 	curp = td->td_proc;
851 	proctree_locked = false;
852 	p2_req_set = false;
853 
854 	/* Lock proctree before locking the process. */
855 	switch (req) {
856 	case PT_TRACE_ME:
857 	case PT_ATTACH:
858 	case PT_STEP:
859 	case PT_CONTINUE:
860 	case PT_TO_SCE:
861 	case PT_TO_SCX:
862 	case PT_SYSCALL:
863 	case PT_FOLLOW_FORK:
864 	case PT_LWP_EVENTS:
865 	case PT_GET_EVENT_MASK:
866 	case PT_SET_EVENT_MASK:
867 	case PT_DETACH:
868 	case PT_GET_SC_ARGS:
869 		sx_xlock(&proctree_lock);
870 		proctree_locked = true;
871 		break;
872 	default:
873 		break;
874 	}
875 
876 	if (req == PT_TRACE_ME) {
877 		p = td->td_proc;
878 		PROC_LOCK(p);
879 	} else {
880 		if (pid <= PID_MAX) {
881 			if ((p = pfind(pid)) == NULL) {
882 				if (proctree_locked)
883 					sx_xunlock(&proctree_lock);
884 				return (ESRCH);
885 			}
886 		} else {
887 			td2 = tdfind(pid, -1);
888 			if (td2 == NULL) {
889 				if (proctree_locked)
890 					sx_xunlock(&proctree_lock);
891 				return (ESRCH);
892 			}
893 			p = td2->td_proc;
894 			tid = pid;
895 			pid = p->p_pid;
896 		}
897 	}
898 	AUDIT_ARG_PROCESS(p);
899 
900 	if ((p->p_flag & P_WEXIT) != 0) {
901 		error = ESRCH;
902 		goto fail;
903 	}
904 	if ((error = p_cansee(td, p)) != 0)
905 		goto fail;
906 
907 	if ((error = p_candebug(td, p)) != 0)
908 		goto fail;
909 
910 	/*
911 	 * System processes can't be debugged.
912 	 */
913 	if ((p->p_flag & P_SYSTEM) != 0) {
914 		error = EINVAL;
915 		goto fail;
916 	}
917 
918 	if (tid == 0) {
919 		if ((p->p_flag & P_STOPPED_TRACE) != 0) {
920 			KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
921 			td2 = p->p_xthread;
922 		} else {
923 			td2 = FIRST_THREAD_IN_PROC(p);
924 		}
925 		tid = td2->td_tid;
926 	}
927 
928 #ifdef COMPAT_FREEBSD32
929 	/*
930 	 * Test if we're a 32 bit client and what the target is.
931 	 * Set the wrap controls accordingly.
932 	 */
933 	if (SV_CURPROC_FLAG(SV_ILP32)) {
934 		if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
935 			safe = 1;
936 		wrap32 = 1;
937 	}
938 #endif
939 	/*
940 	 * Permissions check
941 	 */
942 	switch (req) {
943 	case PT_TRACE_ME:
944 		/*
945 		 * Always legal, when there is a parent process which
946 		 * could trace us.  Otherwise, reject.
947 		 */
948 		if ((p->p_flag & P_TRACED) != 0) {
949 			error = EBUSY;
950 			goto fail;
951 		}
952 		if (p->p_pptr == initproc) {
953 			error = EPERM;
954 			goto fail;
955 		}
956 		break;
957 
958 	case PT_ATTACH:
959 		/* Self */
960 		if (p == td->td_proc) {
961 			error = EINVAL;
962 			goto fail;
963 		}
964 
965 		/* Already traced */
966 		if (p->p_flag & P_TRACED) {
967 			error = EBUSY;
968 			goto fail;
969 		}
970 
971 		/* Can't trace an ancestor if you're being traced. */
972 		if (curp->p_flag & P_TRACED) {
973 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
974 				if (pp == p) {
975 					error = EINVAL;
976 					goto fail;
977 				}
978 			}
979 		}
980 
981 		/* OK */
982 		break;
983 
984 	case PT_CLEARSTEP:
985 		/* Allow thread to clear single step for itself */
986 		if (td->td_tid == tid)
987 			break;
988 
989 		/* FALLTHROUGH */
990 	default:
991 		/*
992 		 * Check for ptrace eligibility before waiting for
993 		 * holds to drain.
994 		 */
995 		error = proc_can_ptrace(td, p);
996 		if (error != 0)
997 			goto fail;
998 
999 		/*
1000 		 * Block parallel ptrace requests.  Most important, do
1001 		 * not allow other thread in debugger to continue the
1002 		 * debuggee until coredump finished.
1003 		 */
1004 		while ((p->p_flag2 & P2_PTRACEREQ) != 0) {
1005 			if (proctree_locked)
1006 				sx_xunlock(&proctree_lock);
1007 			error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH |
1008 			    (proctree_locked ? PDROP : 0), "pptrace", 0);
1009 			if (proctree_locked) {
1010 				sx_xlock(&proctree_lock);
1011 				PROC_LOCK(p);
1012 			}
1013 			if (error == 0 && td2->td_proc != p)
1014 				error = ESRCH;
1015 			if (error == 0)
1016 				error = proc_can_ptrace(td, p);
1017 			if (error != 0)
1018 				goto fail;
1019 		}
1020 
1021 		/* Ok */
1022 		break;
1023 	}
1024 
1025 	/*
1026 	 * Keep this process around and request parallel ptrace()
1027 	 * request to wait until we finish this request.
1028 	 */
1029 	MPASS((p->p_flag2 & P2_PTRACEREQ) == 0);
1030 	p->p_flag2 |= P2_PTRACEREQ;
1031 	p2_req_set = true;
1032 	_PHOLD(p);
1033 
1034 	/*
1035 	 * Actually do the requests
1036 	 */
1037 
1038 	td->td_retval[0] = 0;
1039 
1040 	switch (req) {
1041 	case PT_TRACE_ME:
1042 		/* set my trace flag and "owner" so it can read/write me */
1043 		proc_set_traced(p, false);
1044 		if (p->p_flag & P_PPWAIT)
1045 			p->p_flag |= P_PPTRACE;
1046 		CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
1047 		break;
1048 
1049 	case PT_ATTACH:
1050 		/* security check done above */
1051 		/*
1052 		 * It would be nice if the tracing relationship was separate
1053 		 * from the parent relationship but that would require
1054 		 * another set of links in the proc struct or for "wait"
1055 		 * to scan the entire proc table.  To make life easier,
1056 		 * we just re-parent the process we're trying to trace.
1057 		 * The old parent is remembered so we can put things back
1058 		 * on a "detach".
1059 		 */
1060 		proc_set_traced(p, true);
1061 		proc_reparent(p, td->td_proc, false);
1062 		CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
1063 		    p->p_oppid);
1064 
1065 		sx_xunlock(&proctree_lock);
1066 		proctree_locked = false;
1067 		MPASS(p->p_xthread == NULL);
1068 		MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
1069 
1070 		/*
1071 		 * If already stopped due to a stop signal, clear the
1072 		 * existing stop before triggering a traced SIGSTOP.
1073 		 */
1074 		if ((p->p_flag & P_STOPPED_SIG) != 0) {
1075 			PROC_SLOCK(p);
1076 			p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
1077 			thread_unsuspend(p);
1078 			PROC_SUNLOCK(p);
1079 		}
1080 
1081 		kern_psignal(p, SIGSTOP);
1082 		break;
1083 
1084 	case PT_CLEARSTEP:
1085 		CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
1086 		    p->p_pid);
1087 		error = ptrace_clear_single_step(td2);
1088 		break;
1089 
1090 	case PT_SETSTEP:
1091 		CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
1092 		    p->p_pid);
1093 		error = ptrace_single_step(td2);
1094 		break;
1095 
1096 	case PT_SUSPEND:
1097 		CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
1098 		    p->p_pid);
1099 		td2->td_dbgflags |= TDB_SUSPEND;
1100 		ast_sched(td2, TDA_SUSPEND);
1101 		break;
1102 
1103 	case PT_RESUME:
1104 		CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
1105 		    p->p_pid);
1106 		td2->td_dbgflags &= ~TDB_SUSPEND;
1107 		break;
1108 
1109 	case PT_FOLLOW_FORK:
1110 		CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
1111 		    p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
1112 		    data ? "enabled" : "disabled");
1113 		if (data)
1114 			p->p_ptevents |= PTRACE_FORK;
1115 		else
1116 			p->p_ptevents &= ~PTRACE_FORK;
1117 		break;
1118 
1119 	case PT_LWP_EVENTS:
1120 		CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
1121 		    p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
1122 		    data ? "enabled" : "disabled");
1123 		if (data)
1124 			p->p_ptevents |= PTRACE_LWP;
1125 		else
1126 			p->p_ptevents &= ~PTRACE_LWP;
1127 		break;
1128 
1129 	case PT_GET_EVENT_MASK:
1130 		if (data != sizeof(p->p_ptevents)) {
1131 			error = EINVAL;
1132 			break;
1133 		}
1134 		CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
1135 		    p->p_ptevents);
1136 		*(int *)addr = p->p_ptevents;
1137 		break;
1138 
1139 	case PT_SET_EVENT_MASK:
1140 		if (data != sizeof(p->p_ptevents)) {
1141 			error = EINVAL;
1142 			break;
1143 		}
1144 		tmp = *(int *)addr;
1145 		if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
1146 		    PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
1147 			error = EINVAL;
1148 			break;
1149 		}
1150 		CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
1151 		    p->p_pid, p->p_ptevents, tmp);
1152 		p->p_ptevents = tmp;
1153 		break;
1154 
1155 	case PT_GET_SC_ARGS:
1156 		CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
1157 		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
1158 #ifdef COMPAT_FREEBSD32
1159 		    || (wrap32 && !safe)
1160 #endif
1161 		    ) {
1162 			error = EINVAL;
1163 			break;
1164 		}
1165 		bzero(addr, sizeof(td2->td_sa.args));
1166 		/* See the explanation in linux_ptrace_get_syscall_info(). */
1167 		bcopy(td2->td_sa.args, addr, SV_PROC_ABI(td->td_proc) ==
1168 		    SV_ABI_LINUX ? sizeof(td2->td_sa.args) :
1169 		    td2->td_sa.callp->sy_narg * sizeof(syscallarg_t));
1170 		break;
1171 
1172 	case PT_GET_SC_RET:
1173 		if ((td2->td_dbgflags & (TDB_SCX)) == 0
1174 #ifdef COMPAT_FREEBSD32
1175 		    || (wrap32 && !safe)
1176 #endif
1177 		    ) {
1178 			error = EINVAL;
1179 			break;
1180 		}
1181 		psr = addr;
1182 		bzero(psr, sizeof(*psr));
1183 		psr->sr_error = td2->td_errno;
1184 		if (psr->sr_error == 0) {
1185 			psr->sr_retval[0] = td2->td_retval[0];
1186 			psr->sr_retval[1] = td2->td_retval[1];
1187 		}
1188 		CTR4(KTR_PTRACE,
1189 		    "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
1190 		    p->p_pid, psr->sr_error, psr->sr_retval[0],
1191 		    psr->sr_retval[1]);
1192 		break;
1193 
1194 	case PT_STEP:
1195 	case PT_CONTINUE:
1196 	case PT_TO_SCE:
1197 	case PT_TO_SCX:
1198 	case PT_SYSCALL:
1199 	case PT_DETACH:
1200 		/* Zero means do not send any signal */
1201 		if (data < 0 || data > _SIG_MAXSIG) {
1202 			error = EINVAL;
1203 			break;
1204 		}
1205 
1206 		switch (req) {
1207 		case PT_STEP:
1208 			CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1209 			    td2->td_tid, p->p_pid, data);
1210 			error = ptrace_single_step(td2);
1211 			if (error)
1212 				goto out;
1213 			break;
1214 		case PT_CONTINUE:
1215 		case PT_TO_SCE:
1216 		case PT_TO_SCX:
1217 		case PT_SYSCALL:
1218 			if (addr != (void *)1) {
1219 				error = ptrace_set_pc(td2,
1220 				    (u_long)(uintfptr_t)addr);
1221 				if (error)
1222 					goto out;
1223 			}
1224 			switch (req) {
1225 			case PT_TO_SCE:
1226 				p->p_ptevents |= PTRACE_SCE;
1227 				CTR4(KTR_PTRACE,
1228 		    "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1229 				    p->p_pid, p->p_ptevents,
1230 				    (u_long)(uintfptr_t)addr, data);
1231 				break;
1232 			case PT_TO_SCX:
1233 				p->p_ptevents |= PTRACE_SCX;
1234 				CTR4(KTR_PTRACE,
1235 		    "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1236 				    p->p_pid, p->p_ptevents,
1237 				    (u_long)(uintfptr_t)addr, data);
1238 				break;
1239 			case PT_SYSCALL:
1240 				p->p_ptevents |= PTRACE_SYSCALL;
1241 				CTR4(KTR_PTRACE,
1242 		    "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1243 				    p->p_pid, p->p_ptevents,
1244 				    (u_long)(uintfptr_t)addr, data);
1245 				break;
1246 			case PT_CONTINUE:
1247 				CTR3(KTR_PTRACE,
1248 				    "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1249 				    p->p_pid, (u_long)(uintfptr_t)addr, data);
1250 				break;
1251 			}
1252 			break;
1253 		case PT_DETACH:
1254 			/*
1255 			 * Clear P_TRACED before reparenting
1256 			 * a detached process back to its original
1257 			 * parent.  Otherwise the debugee will be set
1258 			 * as an orphan of the debugger.
1259 			 */
1260 			p->p_flag &= ~(P_TRACED | P_WAITED);
1261 
1262 			/*
1263 			 * Reset the process parent.
1264 			 */
1265 			if (p->p_oppid != p->p_pptr->p_pid) {
1266 				PROC_LOCK(p->p_pptr);
1267 				sigqueue_take(p->p_ksi);
1268 				PROC_UNLOCK(p->p_pptr);
1269 
1270 				pp = proc_realparent(p);
1271 				proc_reparent(p, pp, false);
1272 				if (pp == initproc)
1273 					p->p_sigparent = SIGCHLD;
1274 				CTR3(KTR_PTRACE,
1275 			    "PT_DETACH: pid %d reparented to pid %d, sig %d",
1276 				    p->p_pid, pp->p_pid, data);
1277 			} else {
1278 				CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1279 				    p->p_pid, data);
1280 			}
1281 
1282 			p->p_ptevents = 0;
1283 			FOREACH_THREAD_IN_PROC(p, td3) {
1284 				if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1285 					sigqueue_delete(&td3->td_sigqueue,
1286 					    SIGSTOP);
1287 				}
1288 				td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1289 				    TDB_SUSPEND | TDB_BORN);
1290 			}
1291 
1292 			if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1293 				sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1294 				p->p_flag2 &= ~P2_PTRACE_FSTP;
1295 			}
1296 
1297 			/* should we send SIGCHLD? */
1298 			/* childproc_continued(p); */
1299 			break;
1300 		}
1301 
1302 		sx_xunlock(&proctree_lock);
1303 		proctree_locked = false;
1304 
1305 	sendsig:
1306 		MPASS(!proctree_locked);
1307 
1308 		/*
1309 		 * Clear the pending event for the thread that just
1310 		 * reported its event (p_xthread).  This may not be
1311 		 * the thread passed to PT_CONTINUE, PT_STEP, etc. if
1312 		 * the debugger is resuming a different thread.
1313 		 *
1314 		 * Deliver any pending signal via the reporting thread.
1315 		 */
1316 		MPASS(p->p_xthread != NULL);
1317 		p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1318 		p->p_xthread->td_xsig = data;
1319 		p->p_xthread = NULL;
1320 		p->p_xsig = data;
1321 
1322 		/*
1323 		 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1324 		 * always works immediately, even if another thread is
1325 		 * unsuspended first and attempts to handle a
1326 		 * different signal or if the POSIX.1b style signal
1327 		 * queue cannot accommodate any new signals.
1328 		 */
1329 		if (data == SIGKILL)
1330 			proc_wkilled(p);
1331 
1332 		/*
1333 		 * Unsuspend all threads.  To leave a thread
1334 		 * suspended, use PT_SUSPEND to suspend it before
1335 		 * continuing the process.
1336 		 */
1337 		ptrace_unsuspend(p);
1338 		break;
1339 
1340 	case PT_WRITE_I:
1341 	case PT_WRITE_D:
1342 		td2->td_dbgflags |= TDB_USERWR;
1343 		PROC_UNLOCK(p);
1344 		error = 0;
1345 		if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1346 		    sizeof(int)) != sizeof(int))
1347 			error = ENOMEM;
1348 		else
1349 			CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1350 			    p->p_pid, addr, data);
1351 		PROC_LOCK(p);
1352 		break;
1353 
1354 	case PT_READ_I:
1355 	case PT_READ_D:
1356 		PROC_UNLOCK(p);
1357 		error = tmp = 0;
1358 		if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1359 		    sizeof(int)) != sizeof(int))
1360 			error = ENOMEM;
1361 		else
1362 			CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1363 			    p->p_pid, addr, tmp);
1364 		td->td_retval[0] = tmp;
1365 		PROC_LOCK(p);
1366 		break;
1367 
1368 	case PT_IO:
1369 		piod = addr;
1370 		iov.iov_base = piod->piod_addr;
1371 		iov.iov_len = piod->piod_len;
1372 		uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1373 		uio.uio_resid = piod->piod_len;
1374 		uio.uio_iov = &iov;
1375 		uio.uio_iovcnt = 1;
1376 		uio.uio_segflg = UIO_USERSPACE;
1377 		uio.uio_td = td;
1378 		switch (piod->piod_op) {
1379 		case PIOD_READ_D:
1380 		case PIOD_READ_I:
1381 			CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1382 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1383 			uio.uio_rw = UIO_READ;
1384 			break;
1385 		case PIOD_WRITE_D:
1386 		case PIOD_WRITE_I:
1387 			CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1388 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1389 			td2->td_dbgflags |= TDB_USERWR;
1390 			uio.uio_rw = UIO_WRITE;
1391 			break;
1392 		default:
1393 			error = EINVAL;
1394 			goto out;
1395 		}
1396 		PROC_UNLOCK(p);
1397 		error = proc_rwmem(p, &uio);
1398 		piod->piod_len -= uio.uio_resid;
1399 		PROC_LOCK(p);
1400 		break;
1401 
1402 	case PT_KILL:
1403 		CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1404 		data = SIGKILL;
1405 		goto sendsig;	/* in PT_CONTINUE above */
1406 
1407 	case PT_SETREGS:
1408 		CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1409 		    p->p_pid);
1410 		td2->td_dbgflags |= TDB_USERWR;
1411 		error = PROC_WRITE(regs, td2, addr);
1412 		break;
1413 
1414 	case PT_GETREGS:
1415 		CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1416 		    p->p_pid);
1417 		error = PROC_READ(regs, td2, addr);
1418 		break;
1419 
1420 	case PT_SETFPREGS:
1421 		CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1422 		    p->p_pid);
1423 		td2->td_dbgflags |= TDB_USERWR;
1424 		error = PROC_WRITE(fpregs, td2, addr);
1425 		break;
1426 
1427 	case PT_GETFPREGS:
1428 		CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1429 		    p->p_pid);
1430 		error = PROC_READ(fpregs, td2, addr);
1431 		break;
1432 
1433 	case PT_SETDBREGS:
1434 		CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1435 		    p->p_pid);
1436 		td2->td_dbgflags |= TDB_USERWR;
1437 		error = PROC_WRITE(dbregs, td2, addr);
1438 		break;
1439 
1440 	case PT_GETDBREGS:
1441 		CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1442 		    p->p_pid);
1443 		error = PROC_READ(dbregs, td2, addr);
1444 		break;
1445 
1446 	case PT_SETREGSET:
1447 		CTR2(KTR_PTRACE, "PT_SETREGSET: tid %d (pid %d)", td2->td_tid,
1448 		    p->p_pid);
1449 		error = proc_write_regset(td2, data, addr);
1450 		break;
1451 
1452 	case PT_GETREGSET:
1453 		CTR2(KTR_PTRACE, "PT_GETREGSET: tid %d (pid %d)", td2->td_tid,
1454 		    p->p_pid);
1455 		error = proc_read_regset(td2, data, addr);
1456 		break;
1457 
1458 	case PT_LWPINFO:
1459 		if (data <= 0 || data > sizeof(*pl)) {
1460 			error = EINVAL;
1461 			break;
1462 		}
1463 		pl = addr;
1464 		bzero(pl, sizeof(*pl));
1465 		pl->pl_lwpid = td2->td_tid;
1466 		pl->pl_event = PL_EVENT_NONE;
1467 		pl->pl_flags = 0;
1468 		if (td2->td_dbgflags & TDB_XSIG) {
1469 			pl->pl_event = PL_EVENT_SIGNAL;
1470 			if (td2->td_si.si_signo != 0 &&
1471 			    data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1472 			    + sizeof(pl->pl_siginfo)){
1473 				pl->pl_flags |= PL_FLAG_SI;
1474 				pl->pl_siginfo = td2->td_si;
1475 			}
1476 		}
1477 		if (td2->td_dbgflags & TDB_SCE)
1478 			pl->pl_flags |= PL_FLAG_SCE;
1479 		else if (td2->td_dbgflags & TDB_SCX)
1480 			pl->pl_flags |= PL_FLAG_SCX;
1481 		if (td2->td_dbgflags & TDB_EXEC)
1482 			pl->pl_flags |= PL_FLAG_EXEC;
1483 		if (td2->td_dbgflags & TDB_FORK) {
1484 			pl->pl_flags |= PL_FLAG_FORKED;
1485 			pl->pl_child_pid = td2->td_dbg_forked;
1486 			if (td2->td_dbgflags & TDB_VFORK)
1487 				pl->pl_flags |= PL_FLAG_VFORKED;
1488 		} else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1489 		    TDB_VFORK)
1490 			pl->pl_flags |= PL_FLAG_VFORK_DONE;
1491 		if (td2->td_dbgflags & TDB_CHILD)
1492 			pl->pl_flags |= PL_FLAG_CHILD;
1493 		if (td2->td_dbgflags & TDB_BORN)
1494 			pl->pl_flags |= PL_FLAG_BORN;
1495 		if (td2->td_dbgflags & TDB_EXIT)
1496 			pl->pl_flags |= PL_FLAG_EXITED;
1497 		pl->pl_sigmask = td2->td_sigmask;
1498 		pl->pl_siglist = td2->td_siglist;
1499 		strcpy(pl->pl_tdname, td2->td_name);
1500 		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
1501 			pl->pl_syscall_code = td2->td_sa.code;
1502 			pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
1503 		} else {
1504 			pl->pl_syscall_code = 0;
1505 			pl->pl_syscall_narg = 0;
1506 		}
1507 		CTR6(KTR_PTRACE,
1508     "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1509 		    td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1510 		    pl->pl_child_pid, pl->pl_syscall_code);
1511 		break;
1512 
1513 	case PT_GETNUMLWPS:
1514 		CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1515 		    p->p_numthreads);
1516 		td->td_retval[0] = p->p_numthreads;
1517 		break;
1518 
1519 	case PT_GETLWPLIST:
1520 		CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1521 		    p->p_pid, data, p->p_numthreads);
1522 		if (data <= 0) {
1523 			error = EINVAL;
1524 			break;
1525 		}
1526 		num = imin(p->p_numthreads, data);
1527 		PROC_UNLOCK(p);
1528 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1529 		tmp = 0;
1530 		PROC_LOCK(p);
1531 		FOREACH_THREAD_IN_PROC(p, td2) {
1532 			if (tmp >= num)
1533 				break;
1534 			buf[tmp++] = td2->td_tid;
1535 		}
1536 		PROC_UNLOCK(p);
1537 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1538 		free(buf, M_TEMP);
1539 		if (!error)
1540 			td->td_retval[0] = tmp;
1541 		PROC_LOCK(p);
1542 		break;
1543 
1544 	case PT_VM_TIMESTAMP:
1545 		CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1546 		    p->p_pid, p->p_vmspace->vm_map.timestamp);
1547 		td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1548 		break;
1549 
1550 	case PT_VM_ENTRY:
1551 		PROC_UNLOCK(p);
1552 		error = ptrace_vm_entry(td, p, addr);
1553 		PROC_LOCK(p);
1554 		break;
1555 
1556 	case PT_COREDUMP:
1557 		pc = addr;
1558 		CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d",
1559 		    p->p_pid, pc->pc_fd);
1560 
1561 		if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) {
1562 			error = EINVAL;
1563 			break;
1564 		}
1565 		PROC_UNLOCK(p);
1566 
1567 		tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO);
1568 		fp = NULL;
1569 		error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp);
1570 		if (error != 0)
1571 			goto coredump_cleanup_nofp;
1572 		if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) {
1573 			error = EPIPE;
1574 			goto coredump_cleanup;
1575 		}
1576 
1577 		PROC_LOCK(p);
1578 		error = proc_can_ptrace(td, p);
1579 		if (error != 0)
1580 			goto coredump_cleanup_locked;
1581 
1582 		td2 = ptrace_sel_coredump_thread(p);
1583 		if (td2 == NULL) {
1584 			error = EBUSY;
1585 			goto coredump_cleanup_locked;
1586 		}
1587 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1588 		    TDB_SCREMOTEREQ)) == 0,
1589 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1590 
1591 		tcq->tc_vp = fp->f_vnode;
1592 		tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit;
1593 		tcq->tc_flags = SVC_PT_COREDUMP;
1594 		if ((pc->pc_flags & PC_COMPRESS) == 0)
1595 			tcq->tc_flags |= SVC_NOCOMPRESS;
1596 		if ((pc->pc_flags & PC_ALL) != 0)
1597 			tcq->tc_flags |= SVC_ALL;
1598 		td2->td_remotereq = tcq;
1599 		td2->td_dbgflags |= TDB_COREDUMPREQ;
1600 		thread_run_flash(td2);
1601 		while ((td2->td_dbgflags & TDB_COREDUMPREQ) != 0)
1602 			msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0);
1603 		error = tcq->tc_error;
1604 coredump_cleanup_locked:
1605 		PROC_UNLOCK(p);
1606 coredump_cleanup:
1607 		fdrop(fp, td);
1608 coredump_cleanup_nofp:
1609 		free(tcq, M_TEMP);
1610 		PROC_LOCK(p);
1611 		break;
1612 
1613 	case PT_SC_REMOTE:
1614 		pscr = addr;
1615 		CTR2(KTR_PTRACE, "PT_SC_REMOTE: pid %d, syscall %d",
1616 		    p->p_pid, pscr->pscr_syscall);
1617 		if ((td2->td_dbgflags & TDB_BOUNDARY) == 0) {
1618 			error = EBUSY;
1619 			break;
1620 		}
1621 		PROC_UNLOCK(p);
1622 		MPASS(pscr->pscr_nargs <= nitems(td->td_sa.args));
1623 
1624 		tsr = malloc(sizeof(struct thr_syscall_req), M_TEMP,
1625 		    M_WAITOK | M_ZERO);
1626 
1627 		tsr->ts_sa.code = pscr->pscr_syscall;
1628 		tsr->ts_nargs = pscr->pscr_nargs;
1629 		memcpy(&tsr->ts_sa.args, pscr->pscr_args,
1630 		    sizeof(syscallarg_t) * tsr->ts_nargs);
1631 
1632 		PROC_LOCK(p);
1633 		error = proc_can_ptrace(td, p);
1634 		if (error != 0) {
1635 			free(tsr, M_TEMP);
1636 			break;
1637 		}
1638 		if (td2->td_proc != p) {
1639 			free(tsr, M_TEMP);
1640 			error = ESRCH;
1641 			break;
1642 		}
1643 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1644 		    TDB_SCREMOTEREQ)) == 0,
1645 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1646 
1647 		td2->td_remotereq = tsr;
1648 		td2->td_dbgflags |= TDB_SCREMOTEREQ;
1649 		thread_run_flash(td2);
1650 		while ((td2->td_dbgflags & TDB_SCREMOTEREQ) != 0)
1651 			msleep(p, &p->p_mtx, PPAUSE, "pscrx", 0);
1652 		error = 0;
1653 		memcpy(&pscr->pscr_ret, &tsr->ts_ret, sizeof(tsr->ts_ret));
1654 		free(tsr, M_TEMP);
1655 		break;
1656 
1657 	default:
1658 #ifdef __HAVE_PTRACE_MACHDEP
1659 		if (req >= PT_FIRSTMACH) {
1660 			PROC_UNLOCK(p);
1661 			error = cpu_ptrace(td2, req, addr, data);
1662 			PROC_LOCK(p);
1663 		} else
1664 #endif
1665 			/* Unknown request. */
1666 			error = EINVAL;
1667 		break;
1668 	}
1669 out:
1670 	/* Drop our hold on this process now that the request has completed. */
1671 	_PRELE(p);
1672 fail:
1673 	if (p2_req_set) {
1674 		if ((p->p_flag2 & P2_PTRACEREQ) != 0)
1675 			wakeup(&p->p_flag2);
1676 		p->p_flag2 &= ~P2_PTRACEREQ;
1677 	}
1678 	PROC_UNLOCK(p);
1679 	if (proctree_locked)
1680 		sx_xunlock(&proctree_lock);
1681 	return (error);
1682 }
1683 #undef PROC_READ
1684 #undef PROC_WRITE
1685