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