xref: /freebsd-14.2/sys/kern/sys_process.c (revision 7850fa71)
1 /*-
2  * Copyright (c) 1994, Sean Eric Fagan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Sean Eric Fagan.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/ptrace.h>
47 #include <sys/sx.h>
48 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
50 
51 #include <machine/reg.h>
52 
53 #include <security/audit/audit.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_param.h>
63 
64 #ifdef COMPAT_IA32
65 #include <sys/procfs.h>
66 #include <machine/fpu.h>
67 #include <compat/ia32/ia32_reg.h>
68 
69 struct ptrace_io_desc32 {
70 	int		piod_op;
71 	u_int32_t	piod_offs;
72 	u_int32_t	piod_addr;
73 	u_int32_t	piod_len;
74 };
75 #endif
76 
77 /*
78  * Functions implemented using PROC_ACTION():
79  *
80  * proc_read_regs(proc, regs)
81  *	Get the current user-visible register set from the process
82  *	and copy it into the regs structure (<machine/reg.h>).
83  *	The process is stopped at the time read_regs is called.
84  *
85  * proc_write_regs(proc, regs)
86  *	Update the current register set from the passed in regs
87  *	structure.  Take care to avoid clobbering special CPU
88  *	registers or privileged bits in the PSL.
89  *	Depending on the architecture this may have fix-up work to do,
90  *	especially if the IAR or PCW are modified.
91  *	The process is stopped at the time write_regs is called.
92  *
93  * proc_read_fpregs, proc_write_fpregs
94  *	deal with the floating point register set, otherwise as above.
95  *
96  * proc_read_dbregs, proc_write_dbregs
97  *	deal with the processor debug register set, otherwise as above.
98  *
99  * proc_sstep(proc)
100  *	Arrange for the process to trap after executing a single instruction.
101  */
102 
103 #define	PROC_ACTION(action) do {					\
104 	int error;							\
105 									\
106 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);			\
107 	if ((td->td_proc->p_flag & P_INMEM) == 0)			\
108 		error = EIO;						\
109 	else								\
110 		error = (action);					\
111 	return (error);							\
112 } while(0)
113 
114 int
115 proc_read_regs(struct thread *td, struct reg *regs)
116 {
117 
118 	PROC_ACTION(fill_regs(td, regs));
119 }
120 
121 int
122 proc_write_regs(struct thread *td, struct reg *regs)
123 {
124 
125 	PROC_ACTION(set_regs(td, regs));
126 }
127 
128 int
129 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
130 {
131 
132 	PROC_ACTION(fill_dbregs(td, dbregs));
133 }
134 
135 int
136 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
137 {
138 
139 	PROC_ACTION(set_dbregs(td, dbregs));
140 }
141 
142 /*
143  * Ptrace doesn't support fpregs at all, and there are no security holes
144  * or translations for fpregs, so we can just copy them.
145  */
146 int
147 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
148 {
149 
150 	PROC_ACTION(fill_fpregs(td, fpregs));
151 }
152 
153 int
154 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
155 {
156 
157 	PROC_ACTION(set_fpregs(td, fpregs));
158 }
159 
160 #ifdef COMPAT_IA32
161 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
162 int
163 proc_read_regs32(struct thread *td, struct reg32 *regs32)
164 {
165 
166 	PROC_ACTION(fill_regs32(td, regs32));
167 }
168 
169 int
170 proc_write_regs32(struct thread *td, struct reg32 *regs32)
171 {
172 
173 	PROC_ACTION(set_regs32(td, regs32));
174 }
175 
176 int
177 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
178 {
179 
180 	PROC_ACTION(fill_dbregs32(td, dbregs32));
181 }
182 
183 int
184 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
185 {
186 
187 	PROC_ACTION(set_dbregs32(td, dbregs32));
188 }
189 
190 int
191 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
192 {
193 
194 	PROC_ACTION(fill_fpregs32(td, fpregs32));
195 }
196 
197 int
198 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
199 {
200 
201 	PROC_ACTION(set_fpregs32(td, fpregs32));
202 }
203 #endif
204 
205 int
206 proc_sstep(struct thread *td)
207 {
208 
209 	PROC_ACTION(ptrace_single_step(td));
210 }
211 
212 int
213 proc_rwmem(struct proc *p, struct uio *uio)
214 {
215 	vm_map_t map;
216 	vm_object_t backing_object, object = NULL;
217 	vm_offset_t pageno = 0;		/* page number */
218 	vm_prot_t reqprot;
219 	int error, fault_flags, writing;
220 
221 	/*
222 	 * Assert that someone has locked this vmspace.  (Should be
223 	 * curthread but we can't assert that.)  This keeps the process
224 	 * from exiting out from under us until this operation completes.
225 	 */
226 	KASSERT(p->p_lock >= 1, ("%s: process %p (pid %d) not held", __func__,
227 	    p, p->p_pid));
228 
229 	/*
230 	 * The map we want...
231 	 */
232 	map = &p->p_vmspace->vm_map;
233 
234 	writing = uio->uio_rw == UIO_WRITE;
235 	reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) :
236 	    VM_PROT_READ;
237 	fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
238 
239 	/*
240 	 * Only map in one page at a time.  We don't have to, but it
241 	 * makes things easier.  This way is trivial - right?
242 	 */
243 	do {
244 		vm_map_t tmap;
245 		vm_offset_t uva;
246 		int page_offset;		/* offset into page */
247 		vm_map_entry_t out_entry;
248 		vm_prot_t out_prot;
249 		boolean_t wired;
250 		vm_pindex_t pindex;
251 		u_int len;
252 		vm_page_t m;
253 
254 		object = NULL;
255 
256 		uva = (vm_offset_t)uio->uio_offset;
257 
258 		/*
259 		 * Get the page number of this segment.
260 		 */
261 		pageno = trunc_page(uva);
262 		page_offset = uva - pageno;
263 
264 		/*
265 		 * How many bytes to copy
266 		 */
267 		len = min(PAGE_SIZE - page_offset, uio->uio_resid);
268 
269 		/*
270 		 * Fault the page on behalf of the process
271 		 */
272 		error = vm_fault(map, pageno, reqprot, fault_flags);
273 		if (error) {
274 			if (error == KERN_RESOURCE_SHORTAGE)
275 				error = ENOMEM;
276 			else
277 				error = EFAULT;
278 			break;
279 		}
280 
281 		/*
282 		 * Now we need to get the page.  out_entry, out_prot, wired,
283 		 * and single_use aren't used.  One would think the vm code
284 		 * would be a *bit* nicer...  We use tmap because
285 		 * vm_map_lookup() can change the map argument.
286 		 */
287 		tmap = map;
288 		error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
289 		    &object, &pindex, &out_prot, &wired);
290 		if (error) {
291 			error = EFAULT;
292 			break;
293 		}
294 		VM_OBJECT_LOCK(object);
295 		while ((m = vm_page_lookup(object, pindex)) == NULL &&
296 		    !writing &&
297 		    (backing_object = object->backing_object) != NULL) {
298 			/*
299 			 * Allow fallback to backing objects if we are reading.
300 			 */
301 			VM_OBJECT_LOCK(backing_object);
302 			pindex += OFF_TO_IDX(object->backing_object_offset);
303 			VM_OBJECT_UNLOCK(object);
304 			object = backing_object;
305 		}
306 		VM_OBJECT_UNLOCK(object);
307 		if (m == NULL) {
308 			vm_map_lookup_done(tmap, out_entry);
309 			error = EFAULT;
310 			break;
311 		}
312 
313 		/*
314 		 * Hold the page in memory.
315 		 */
316 		vm_page_lock_queues();
317 		vm_page_hold(m);
318 		vm_page_unlock_queues();
319 
320 		/*
321 		 * We're done with tmap now.
322 		 */
323 		vm_map_lookup_done(tmap, out_entry);
324 
325 		/*
326 		 * Now do the i/o move.
327 		 */
328 		error = uiomove_fromphys(&m, page_offset, len, uio);
329 
330 		/*
331 		 * Release the page.
332 		 */
333 		vm_page_lock_queues();
334 		vm_page_unhold(m);
335 		vm_page_unlock_queues();
336 
337 	} while (error == 0 && uio->uio_resid > 0);
338 
339 	return (error);
340 }
341 
342 /*
343  * Process debugging system call.
344  */
345 #ifndef _SYS_SYSPROTO_H_
346 struct ptrace_args {
347 	int	req;
348 	pid_t	pid;
349 	caddr_t	addr;
350 	int	data;
351 };
352 #endif
353 
354 #ifdef COMPAT_IA32
355 /*
356  * This CPP subterfuge is to try and reduce the number of ifdefs in
357  * the body of the code.
358  *   COPYIN(uap->addr, &r.reg, sizeof r.reg);
359  * becomes either:
360  *   copyin(uap->addr, &r.reg, sizeof r.reg);
361  * or
362  *   copyin(uap->addr, &r.reg32, sizeof r.reg32);
363  * .. except this is done at runtime.
364  */
365 #define	COPYIN(u, k, s)		wrap32 ? \
366 	copyin(u, k ## 32, s ## 32) : \
367 	copyin(u, k, s)
368 #define	COPYOUT(k, u, s)	wrap32 ? \
369 	copyout(k ## 32, u, s ## 32) : \
370 	copyout(k, u, s)
371 #else
372 #define	COPYIN(u, k, s)		copyin(u, k, s)
373 #define	COPYOUT(k, u, s)	copyout(k, u, s)
374 #endif
375 int
376 ptrace(struct thread *td, struct ptrace_args *uap)
377 {
378 	/*
379 	 * XXX this obfuscation is to reduce stack usage, but the register
380 	 * structs may be too large to put on the stack anyway.
381 	 */
382 	union {
383 		struct ptrace_io_desc piod;
384 		struct ptrace_lwpinfo pl;
385 		struct dbreg dbreg;
386 		struct fpreg fpreg;
387 		struct reg reg;
388 #ifdef COMPAT_IA32
389 		struct dbreg32 dbreg32;
390 		struct fpreg32 fpreg32;
391 		struct reg32 reg32;
392 		struct ptrace_io_desc32 piod32;
393 #endif
394 	} r;
395 	void *addr;
396 	int error = 0;
397 #ifdef COMPAT_IA32
398 	int wrap32 = 0;
399 
400 	if (SV_CURPROC_FLAG(SV_ILP32))
401 		wrap32 = 1;
402 #endif
403 	AUDIT_ARG_PID(uap->pid);
404 	AUDIT_ARG_CMD(uap->req);
405 	AUDIT_ARG_ADDR(uap->addr);
406 	AUDIT_ARG_VALUE(uap->data);
407 	addr = &r;
408 	switch (uap->req) {
409 	case PT_GETREGS:
410 	case PT_GETFPREGS:
411 	case PT_GETDBREGS:
412 	case PT_LWPINFO:
413 		break;
414 	case PT_SETREGS:
415 		error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
416 		break;
417 	case PT_SETFPREGS:
418 		error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
419 		break;
420 	case PT_SETDBREGS:
421 		error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
422 		break;
423 	case PT_IO:
424 		error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
425 		break;
426 	default:
427 		addr = uap->addr;
428 		break;
429 	}
430 	if (error)
431 		return (error);
432 
433 	error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
434 	if (error)
435 		return (error);
436 
437 	switch (uap->req) {
438 	case PT_IO:
439 		error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
440 		break;
441 	case PT_GETREGS:
442 		error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
443 		break;
444 	case PT_GETFPREGS:
445 		error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
446 		break;
447 	case PT_GETDBREGS:
448 		error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
449 		break;
450 	case PT_LWPINFO:
451 		error = copyout(&r.pl, uap->addr, uap->data);
452 		break;
453 	}
454 
455 	return (error);
456 }
457 #undef COPYIN
458 #undef COPYOUT
459 
460 #ifdef COMPAT_IA32
461 /*
462  *   PROC_READ(regs, td2, addr);
463  * becomes either:
464  *   proc_read_regs(td2, addr);
465  * or
466  *   proc_read_regs32(td2, addr);
467  * .. except this is done at runtime.  There is an additional
468  * complication in that PROC_WRITE disallows 32 bit consumers
469  * from writing to 64 bit address space targets.
470  */
471 #define	PROC_READ(w, t, a)	wrap32 ? \
472 	proc_read_ ## w ## 32(t, a) : \
473 	proc_read_ ## w (t, a)
474 #define	PROC_WRITE(w, t, a)	wrap32 ? \
475 	(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
476 	proc_write_ ## w (t, a)
477 #else
478 #define	PROC_READ(w, t, a)	proc_read_ ## w (t, a)
479 #define	PROC_WRITE(w, t, a)	proc_write_ ## w (t, a)
480 #endif
481 
482 int
483 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
484 {
485 	struct iovec iov;
486 	struct uio uio;
487 	struct proc *curp, *p, *pp;
488 	struct thread *td2 = NULL;
489 	struct ptrace_io_desc *piod = NULL;
490 	struct ptrace_lwpinfo *pl;
491 	int error, write, tmp, num;
492 	int proctree_locked = 0;
493 	lwpid_t tid = 0, *buf;
494 #ifdef COMPAT_IA32
495 	int wrap32 = 0, safe = 0;
496 	struct ptrace_io_desc32 *piod32 = NULL;
497 #endif
498 
499 	curp = td->td_proc;
500 
501 	/* Lock proctree before locking the process. */
502 	switch (req) {
503 	case PT_TRACE_ME:
504 	case PT_ATTACH:
505 	case PT_STEP:
506 	case PT_CONTINUE:
507 	case PT_TO_SCE:
508 	case PT_TO_SCX:
509 	case PT_SYSCALL:
510 	case PT_DETACH:
511 		sx_xlock(&proctree_lock);
512 		proctree_locked = 1;
513 		break;
514 	default:
515 		break;
516 	}
517 
518 	write = 0;
519 	if (req == PT_TRACE_ME) {
520 		p = td->td_proc;
521 		PROC_LOCK(p);
522 	} else {
523 		if (pid <= PID_MAX) {
524 			if ((p = pfind(pid)) == NULL) {
525 				if (proctree_locked)
526 					sx_xunlock(&proctree_lock);
527 				return (ESRCH);
528 			}
529 		} else {
530 			/* this is slow, should be optimized */
531 			sx_slock(&allproc_lock);
532 			FOREACH_PROC_IN_SYSTEM(p) {
533 				PROC_LOCK(p);
534 				FOREACH_THREAD_IN_PROC(p, td2) {
535 					if (td2->td_tid == pid)
536 						break;
537 				}
538 				if (td2 != NULL)
539 					break; /* proc lock held */
540 				PROC_UNLOCK(p);
541 			}
542 			sx_sunlock(&allproc_lock);
543 			if (p == NULL) {
544 				if (proctree_locked)
545 					sx_xunlock(&proctree_lock);
546 				return (ESRCH);
547 			}
548 			tid = pid;
549 			pid = p->p_pid;
550 		}
551 	}
552 	AUDIT_ARG_PROCESS(p);
553 
554 	if ((p->p_flag & P_WEXIT) != 0) {
555 		error = ESRCH;
556 		goto fail;
557 	}
558 	if ((error = p_cansee(td, p)) != 0)
559 		goto fail;
560 
561 	if ((error = p_candebug(td, p)) != 0)
562 		goto fail;
563 
564 	/*
565 	 * System processes can't be debugged.
566 	 */
567 	if ((p->p_flag & P_SYSTEM) != 0) {
568 		error = EINVAL;
569 		goto fail;
570 	}
571 
572 	if (tid == 0) {
573 		if ((p->p_flag & P_STOPPED_TRACE) != 0) {
574 			KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
575 			td2 = p->p_xthread;
576 		} else {
577 			td2 = FIRST_THREAD_IN_PROC(p);
578 		}
579 		tid = td2->td_tid;
580 	}
581 
582 #ifdef COMPAT_IA32
583 	/*
584 	 * Test if we're a 32 bit client and what the target is.
585 	 * Set the wrap controls accordingly.
586 	 */
587 	if (SV_CURPROC_FLAG(SV_ILP32)) {
588 		if (td2->td_proc->p_sysent->sv_flags & SV_ILP32)
589 			safe = 1;
590 		wrap32 = 1;
591 	}
592 #endif
593 	/*
594 	 * Permissions check
595 	 */
596 	switch (req) {
597 	case PT_TRACE_ME:
598 		/* Always legal. */
599 		break;
600 
601 	case PT_ATTACH:
602 		/* Self */
603 		if (p->p_pid == td->td_proc->p_pid) {
604 			error = EINVAL;
605 			goto fail;
606 		}
607 
608 		/* Already traced */
609 		if (p->p_flag & P_TRACED) {
610 			error = EBUSY;
611 			goto fail;
612 		}
613 
614 		/* Can't trace an ancestor if you're being traced. */
615 		if (curp->p_flag & P_TRACED) {
616 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
617 				if (pp == p) {
618 					error = EINVAL;
619 					goto fail;
620 				}
621 			}
622 		}
623 
624 
625 		/* OK */
626 		break;
627 
628 	case PT_CLEARSTEP:
629 		/* Allow thread to clear single step for itself */
630 		if (td->td_tid == tid)
631 			break;
632 
633 		/* FALLTHROUGH */
634 	default:
635 		/* not being traced... */
636 		if ((p->p_flag & P_TRACED) == 0) {
637 			error = EPERM;
638 			goto fail;
639 		}
640 
641 		/* not being traced by YOU */
642 		if (p->p_pptr != td->td_proc) {
643 			error = EBUSY;
644 			goto fail;
645 		}
646 
647 		/* not currently stopped */
648 		if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
649 		    p->p_suspcount != p->p_numthreads  ||
650 		    (p->p_flag & P_WAITED) == 0) {
651 			error = EBUSY;
652 			goto fail;
653 		}
654 
655 		if ((p->p_flag & P_STOPPED_TRACE) == 0) {
656 			static int count = 0;
657 			if (count++ == 0)
658 				printf("P_STOPPED_TRACE not set.\n");
659 		}
660 
661 		/* OK */
662 		break;
663 	}
664 
665 	/* Keep this process around until we finish this request. */
666 	_PHOLD(p);
667 
668 #ifdef FIX_SSTEP
669 	/*
670 	 * Single step fixup ala procfs
671 	 */
672 	FIX_SSTEP(td2);
673 #endif
674 
675 	/*
676 	 * Actually do the requests
677 	 */
678 
679 	td->td_retval[0] = 0;
680 
681 	switch (req) {
682 	case PT_TRACE_ME:
683 		/* set my trace flag and "owner" so it can read/write me */
684 		p->p_flag |= P_TRACED;
685 		p->p_oppid = p->p_pptr->p_pid;
686 		break;
687 
688 	case PT_ATTACH:
689 		/* security check done above */
690 		p->p_flag |= P_TRACED;
691 		p->p_oppid = p->p_pptr->p_pid;
692 		if (p->p_pptr != td->td_proc)
693 			proc_reparent(p, td->td_proc);
694 		data = SIGSTOP;
695 		goto sendsig;	/* in PT_CONTINUE below */
696 
697 	case PT_CLEARSTEP:
698 		error = ptrace_clear_single_step(td2);
699 		break;
700 
701 	case PT_SETSTEP:
702 		error = ptrace_single_step(td2);
703 		break;
704 
705 	case PT_SUSPEND:
706 		td2->td_dbgflags |= TDB_SUSPEND;
707 		thread_lock(td2);
708 		td2->td_flags |= TDF_NEEDSUSPCHK;
709 		thread_unlock(td2);
710 		break;
711 
712 	case PT_RESUME:
713 		td2->td_dbgflags &= ~TDB_SUSPEND;
714 		break;
715 
716 	case PT_STEP:
717 	case PT_CONTINUE:
718 	case PT_TO_SCE:
719 	case PT_TO_SCX:
720 	case PT_SYSCALL:
721 	case PT_DETACH:
722 		/* Zero means do not send any signal */
723 		if (data < 0 || data > _SIG_MAXSIG) {
724 			error = EINVAL;
725 			break;
726 		}
727 
728 		switch (req) {
729 		case PT_STEP:
730 			error = ptrace_single_step(td2);
731 			if (error)
732 				goto out;
733 			break;
734 		case PT_TO_SCE:
735 			p->p_stops |= S_PT_SCE;
736 			break;
737 		case PT_TO_SCX:
738 			p->p_stops |= S_PT_SCX;
739 			break;
740 		case PT_SYSCALL:
741 			p->p_stops |= S_PT_SCE | S_PT_SCX;
742 			break;
743 		}
744 
745 		if (addr != (void *)1) {
746 			error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr);
747 			if (error)
748 				break;
749 		}
750 
751 		if (req == PT_DETACH) {
752 			/* reset process parent */
753 			if (p->p_oppid != p->p_pptr->p_pid) {
754 				struct proc *pp;
755 
756 				PROC_LOCK(p->p_pptr);
757 				sigqueue_take(p->p_ksi);
758 				PROC_UNLOCK(p->p_pptr);
759 
760 				PROC_UNLOCK(p);
761 				pp = pfind(p->p_oppid);
762 				if (pp == NULL)
763 					pp = initproc;
764 				else
765 					PROC_UNLOCK(pp);
766 				PROC_LOCK(p);
767 				proc_reparent(p, pp);
768 				if (pp == initproc)
769 					p->p_sigparent = SIGCHLD;
770 			}
771 			p->p_flag &= ~(P_TRACED | P_WAITED);
772 			p->p_oppid = 0;
773 
774 			/* should we send SIGCHLD? */
775 			/* childproc_continued(p); */
776 		}
777 
778 	sendsig:
779 		if (proctree_locked) {
780 			sx_xunlock(&proctree_lock);
781 			proctree_locked = 0;
782 		}
783 		p->p_xstat = data;
784 		p->p_xthread = NULL;
785 		if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) {
786 			/* deliver or queue signal */
787 			td2->td_dbgflags &= ~TDB_XSIG;
788 			td2->td_xsig = data;
789 
790 			if (req == PT_DETACH) {
791 				struct thread *td3;
792 				FOREACH_THREAD_IN_PROC(p, td3) {
793 					td3->td_dbgflags &= ~TDB_SUSPEND;
794 				}
795 			}
796 			/*
797 			 * unsuspend all threads, to not let a thread run,
798 			 * you should use PT_SUSPEND to suspend it before
799 			 * continuing process.
800 			 */
801 			PROC_SLOCK(p);
802 			p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
803 			thread_unsuspend(p);
804 			PROC_SUNLOCK(p);
805 		} else {
806 			if (data)
807 				psignal(p, data);
808 		}
809 		break;
810 
811 	case PT_WRITE_I:
812 	case PT_WRITE_D:
813 		write = 1;
814 		/* FALLTHROUGH */
815 	case PT_READ_I:
816 	case PT_READ_D:
817 		PROC_UNLOCK(p);
818 		tmp = 0;
819 		/* write = 0 set above */
820 		iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
821 		iov.iov_len = sizeof(int);
822 		uio.uio_iov = &iov;
823 		uio.uio_iovcnt = 1;
824 		uio.uio_offset = (off_t)(uintptr_t)addr;
825 		uio.uio_resid = sizeof(int);
826 		uio.uio_segflg = UIO_SYSSPACE;	/* i.e.: the uap */
827 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
828 		uio.uio_td = td;
829 		error = proc_rwmem(p, &uio);
830 		if (uio.uio_resid != 0) {
831 			/*
832 			 * XXX proc_rwmem() doesn't currently return ENOSPC,
833 			 * so I think write() can bogusly return 0.
834 			 * XXX what happens for short writes?  We don't want
835 			 * to write partial data.
836 			 * XXX proc_rwmem() returns EPERM for other invalid
837 			 * addresses.  Convert this to EINVAL.  Does this
838 			 * clobber returns of EPERM for other reasons?
839 			 */
840 			if (error == 0 || error == ENOSPC || error == EPERM)
841 				error = EINVAL;	/* EOF */
842 		}
843 		if (!write)
844 			td->td_retval[0] = tmp;
845 		PROC_LOCK(p);
846 		break;
847 
848 	case PT_IO:
849 #ifdef COMPAT_IA32
850 		if (wrap32) {
851 			piod32 = addr;
852 			iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
853 			iov.iov_len = piod32->piod_len;
854 			uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
855 			uio.uio_resid = piod32->piod_len;
856 		} else
857 #endif
858 		{
859 			piod = addr;
860 			iov.iov_base = piod->piod_addr;
861 			iov.iov_len = piod->piod_len;
862 			uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
863 			uio.uio_resid = piod->piod_len;
864 		}
865 		uio.uio_iov = &iov;
866 		uio.uio_iovcnt = 1;
867 		uio.uio_segflg = UIO_USERSPACE;
868 		uio.uio_td = td;
869 #ifdef COMPAT_IA32
870 		tmp = wrap32 ? piod32->piod_op : piod->piod_op;
871 #else
872 		tmp = piod->piod_op;
873 #endif
874 		switch (tmp) {
875 		case PIOD_READ_D:
876 		case PIOD_READ_I:
877 			uio.uio_rw = UIO_READ;
878 			break;
879 		case PIOD_WRITE_D:
880 		case PIOD_WRITE_I:
881 			uio.uio_rw = UIO_WRITE;
882 			break;
883 		default:
884 			error = EINVAL;
885 			goto out;
886 		}
887 		PROC_UNLOCK(p);
888 		error = proc_rwmem(p, &uio);
889 #ifdef COMPAT_IA32
890 		if (wrap32)
891 			piod32->piod_len -= uio.uio_resid;
892 		else
893 #endif
894 			piod->piod_len -= uio.uio_resid;
895 		PROC_LOCK(p);
896 		break;
897 
898 	case PT_KILL:
899 		data = SIGKILL;
900 		goto sendsig;	/* in PT_CONTINUE above */
901 
902 	case PT_SETREGS:
903 		error = PROC_WRITE(regs, td2, addr);
904 		break;
905 
906 	case PT_GETREGS:
907 		error = PROC_READ(regs, td2, addr);
908 		break;
909 
910 	case PT_SETFPREGS:
911 		error = PROC_WRITE(fpregs, td2, addr);
912 		break;
913 
914 	case PT_GETFPREGS:
915 		error = PROC_READ(fpregs, td2, addr);
916 		break;
917 
918 	case PT_SETDBREGS:
919 		error = PROC_WRITE(dbregs, td2, addr);
920 		break;
921 
922 	case PT_GETDBREGS:
923 		error = PROC_READ(dbregs, td2, addr);
924 		break;
925 
926 	case PT_LWPINFO:
927 		if (data <= 0 || data > sizeof(*pl)) {
928 			error = EINVAL;
929 			break;
930 		}
931 		pl = addr;
932 		pl->pl_lwpid = td2->td_tid;
933 		if (td2->td_dbgflags & TDB_XSIG)
934 			pl->pl_event = PL_EVENT_SIGNAL;
935 		else
936 			pl->pl_event = 0;
937 		pl->pl_flags = 0;
938 		pl->pl_sigmask = td2->td_sigmask;
939 		pl->pl_siglist = td2->td_siglist;
940 		break;
941 
942 	case PT_GETNUMLWPS:
943 		td->td_retval[0] = p->p_numthreads;
944 		break;
945 
946 	case PT_GETLWPLIST:
947 		if (data <= 0) {
948 			error = EINVAL;
949 			break;
950 		}
951 		num = imin(p->p_numthreads, data);
952 		PROC_UNLOCK(p);
953 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
954 		tmp = 0;
955 		PROC_LOCK(p);
956 		FOREACH_THREAD_IN_PROC(p, td2) {
957 			if (tmp >= num)
958 				break;
959 			buf[tmp++] = td2->td_tid;
960 		}
961 		PROC_UNLOCK(p);
962 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
963 		free(buf, M_TEMP);
964 		if (!error)
965 			td->td_retval[0] = tmp;
966 		PROC_LOCK(p);
967 		break;
968 
969 	default:
970 #ifdef __HAVE_PTRACE_MACHDEP
971 		if (req >= PT_FIRSTMACH) {
972 			PROC_UNLOCK(p);
973 			error = cpu_ptrace(td2, req, addr, data);
974 			PROC_LOCK(p);
975 		} else
976 #endif
977 			/* Unknown request. */
978 			error = EINVAL;
979 		break;
980 	}
981 
982 out:
983 	/* Drop our hold on this process now that the request has completed. */
984 	_PRELE(p);
985 fail:
986 	PROC_UNLOCK(p);
987 	if (proctree_locked)
988 		sx_xunlock(&proctree_lock);
989 	return (error);
990 }
991 #undef PROC_READ
992 #undef PROC_WRITE
993 
994 /*
995  * Stop a process because of a debugging event;
996  * stay stopped until p->p_step is cleared
997  * (cleared by PIOCCONT in procfs).
998  */
999 void
1000 stopevent(struct proc *p, unsigned int event, unsigned int val)
1001 {
1002 
1003 	PROC_LOCK_ASSERT(p, MA_OWNED);
1004 	p->p_step = 1;
1005 	do {
1006 		p->p_xstat = val;
1007 		p->p_xthread = NULL;
1008 		p->p_stype = event;	/* Which event caused the stop? */
1009 		wakeup(&p->p_stype);	/* Wake up any PIOCWAIT'ing procs */
1010 		msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1011 	} while (p->p_step);
1012 }
1013