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