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