xref: /freebsd/sys/kern/sys_process.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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/sysproto.h>
43 #include <sys/proc.h>
44 #include <sys/vnode.h>
45 #include <sys/ptrace.h>
46 #include <sys/sx.h>
47 #include <sys/malloc.h>
48 #include <sys/signalvar.h>
49 
50 #include <machine/reg.h>
51 
52 #include <security/audit/audit.h>
53 
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
61 
62 #ifdef COMPAT_IA32
63 #include <sys/procfs.h>
64 #include <machine/fpu.h>
65 #include <compat/ia32/ia32_reg.h>
66 
67 extern struct sysentvec ia32_freebsd_sysvec;
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 			error = EFAULT;
275 			break;
276 		}
277 
278 		/*
279 		 * Now we need to get the page.  out_entry, out_prot, wired,
280 		 * and single_use aren't used.  One would think the vm code
281 		 * would be a *bit* nicer...  We use tmap because
282 		 * vm_map_lookup() can change the map argument.
283 		 */
284 		tmap = map;
285 		error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
286 		    &object, &pindex, &out_prot, &wired);
287 		if (error) {
288 			error = EFAULT;
289 			break;
290 		}
291 		VM_OBJECT_LOCK(object);
292 		while ((m = vm_page_lookup(object, pindex)) == NULL &&
293 		    !writing &&
294 		    (backing_object = object->backing_object) != NULL) {
295 			/*
296 			 * Allow fallback to backing objects if we are reading.
297 			 */
298 			VM_OBJECT_LOCK(backing_object);
299 			pindex += OFF_TO_IDX(object->backing_object_offset);
300 			VM_OBJECT_UNLOCK(object);
301 			object = backing_object;
302 		}
303 		VM_OBJECT_UNLOCK(object);
304 		if (m == NULL) {
305 			vm_map_lookup_done(tmap, out_entry);
306 			error = EFAULT;
307 			break;
308 		}
309 
310 		/*
311 		 * Hold the page in memory.
312 		 */
313 		vm_page_lock_queues();
314 		vm_page_hold(m);
315 		vm_page_unlock_queues();
316 
317 		/*
318 		 * We're done with tmap now.
319 		 */
320 		vm_map_lookup_done(tmap, out_entry);
321 
322 		/*
323 		 * Now do the i/o move.
324 		 */
325 		error = uiomove_fromphys(&m, page_offset, len, uio);
326 
327 		/*
328 		 * Release the page.
329 		 */
330 		vm_page_lock_queues();
331 		vm_page_unhold(m);
332 		vm_page_unlock_queues();
333 
334 	} while (error == 0 && uio->uio_resid > 0);
335 
336 	return (error);
337 }
338 
339 /*
340  * Process debugging system call.
341  */
342 #ifndef _SYS_SYSPROTO_H_
343 struct ptrace_args {
344 	int	req;
345 	pid_t	pid;
346 	caddr_t	addr;
347 	int	data;
348 };
349 #endif
350 
351 #ifdef COMPAT_IA32
352 /*
353  * This CPP subterfuge is to try and reduce the number of ifdefs in
354  * the body of the code.
355  *   COPYIN(uap->addr, &r.reg, sizeof r.reg);
356  * becomes either:
357  *   copyin(uap->addr, &r.reg, sizeof r.reg);
358  * or
359  *   copyin(uap->addr, &r.reg32, sizeof r.reg32);
360  * .. except this is done at runtime.
361  */
362 #define	COPYIN(u, k, s)		wrap32 ? \
363 	copyin(u, k ## 32, s ## 32) : \
364 	copyin(u, k, s)
365 #define	COPYOUT(k, u, s)	wrap32 ? \
366 	copyout(k ## 32, u, s ## 32) : \
367 	copyout(k, u, s)
368 #else
369 #define	COPYIN(u, k, s)		copyin(u, k, s)
370 #define	COPYOUT(k, u, s)	copyout(k, u, s)
371 #endif
372 int
373 ptrace(struct thread *td, struct ptrace_args *uap)
374 {
375 	/*
376 	 * XXX this obfuscation is to reduce stack usage, but the register
377 	 * structs may be too large to put on the stack anyway.
378 	 */
379 	union {
380 		struct ptrace_io_desc piod;
381 		struct ptrace_lwpinfo pl;
382 		struct dbreg dbreg;
383 		struct fpreg fpreg;
384 		struct reg reg;
385 #ifdef COMPAT_IA32
386 		struct dbreg32 dbreg32;
387 		struct fpreg32 fpreg32;
388 		struct reg32 reg32;
389 		struct ptrace_io_desc32 piod32;
390 #endif
391 	} r;
392 	void *addr;
393 	int error = 0;
394 #ifdef COMPAT_IA32
395 	int wrap32 = 0;
396 
397 	if (td->td_proc->p_sysent == &ia32_freebsd_sysvec)
398 		wrap32 = 1;
399 #endif
400 	AUDIT_ARG(pid, uap->pid);
401 	AUDIT_ARG(cmd, uap->req);
402 	AUDIT_ARG(addr, uap->addr);
403 	AUDIT_ARG(value, uap->data);
404 	addr = &r;
405 	switch (uap->req) {
406 	case PT_GETREGS:
407 	case PT_GETFPREGS:
408 	case PT_GETDBREGS:
409 	case PT_LWPINFO:
410 		break;
411 	case PT_SETREGS:
412 		error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
413 		break;
414 	case PT_SETFPREGS:
415 		error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
416 		break;
417 	case PT_SETDBREGS:
418 		error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
419 		break;
420 	case PT_IO:
421 		error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
422 		break;
423 	default:
424 		addr = uap->addr;
425 		break;
426 	}
427 	if (error)
428 		return (error);
429 
430 	error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
431 	if (error)
432 		return (error);
433 
434 	switch (uap->req) {
435 	case PT_IO:
436 		error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
437 		break;
438 	case PT_GETREGS:
439 		error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
440 		break;
441 	case PT_GETFPREGS:
442 		error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
443 		break;
444 	case PT_GETDBREGS:
445 		error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
446 		break;
447 	case PT_LWPINFO:
448 		error = copyout(&r.pl, uap->addr, uap->data);
449 		break;
450 	}
451 
452 	return (error);
453 }
454 #undef COPYIN
455 #undef COPYOUT
456 
457 #ifdef COMPAT_IA32
458 /*
459  *   PROC_READ(regs, td2, addr);
460  * becomes either:
461  *   proc_read_regs(td2, addr);
462  * or
463  *   proc_read_regs32(td2, addr);
464  * .. except this is done at runtime.  There is an additional
465  * complication in that PROC_WRITE disallows 32 bit consumers
466  * from writing to 64 bit address space targets.
467  */
468 #define	PROC_READ(w, t, a)	wrap32 ? \
469 	proc_read_ ## w ## 32(t, a) : \
470 	proc_read_ ## w (t, a)
471 #define	PROC_WRITE(w, t, a)	wrap32 ? \
472 	(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
473 	proc_write_ ## w (t, a)
474 #else
475 #define	PROC_READ(w, t, a)	proc_read_ ## w (t, a)
476 #define	PROC_WRITE(w, t, a)	proc_write_ ## w (t, a)
477 #endif
478 
479 int
480 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
481 {
482 	struct iovec iov;
483 	struct uio uio;
484 	struct proc *curp, *p, *pp;
485 	struct thread *td2 = NULL;
486 	struct ptrace_io_desc *piod = NULL;
487 	struct ptrace_lwpinfo *pl;
488 	int error, write, tmp, num;
489 	int proctree_locked = 0;
490 	lwpid_t tid = 0, *buf;
491 #ifdef COMPAT_IA32
492 	int wrap32 = 0, safe = 0;
493 	struct ptrace_io_desc32 *piod32 = NULL;
494 #endif
495 
496 	curp = td->td_proc;
497 
498 	/* Lock proctree before locking the process. */
499 	switch (req) {
500 	case PT_TRACE_ME:
501 	case PT_ATTACH:
502 	case PT_STEP:
503 	case PT_CONTINUE:
504 	case PT_TO_SCE:
505 	case PT_TO_SCX:
506 	case PT_SYSCALL:
507 	case PT_DETACH:
508 		sx_xlock(&proctree_lock);
509 		proctree_locked = 1;
510 		break;
511 	default:
512 		break;
513 	}
514 
515 	write = 0;
516 	if (req == PT_TRACE_ME) {
517 		p = td->td_proc;
518 		PROC_LOCK(p);
519 	} else {
520 		if (pid <= PID_MAX) {
521 			if ((p = pfind(pid)) == NULL) {
522 				if (proctree_locked)
523 					sx_xunlock(&proctree_lock);
524 				return (ESRCH);
525 			}
526 		} else {
527 			/* this is slow, should be optimized */
528 			sx_slock(&allproc_lock);
529 			FOREACH_PROC_IN_SYSTEM(p) {
530 				PROC_LOCK(p);
531 				PROC_SLOCK(p);
532 				FOREACH_THREAD_IN_PROC(p, td2) {
533 					if (td2->td_tid == pid)
534 						break;
535 				}
536 				PROC_SUNLOCK(p);
537 				if (td2 != NULL)
538 					break; /* proc lock held */
539 				PROC_UNLOCK(p);
540 			}
541 			sx_sunlock(&allproc_lock);
542 			if (p == NULL) {
543 				if (proctree_locked)
544 					sx_xunlock(&proctree_lock);
545 				return (ESRCH);
546 			}
547 			tid = pid;
548 			pid = p->p_pid;
549 		}
550 	}
551 	AUDIT_ARG(process, p);
552 
553 	if ((p->p_flag & P_WEXIT) != 0) {
554 		error = ESRCH;
555 		goto fail;
556 	}
557 	if ((error = p_cansee(td, p)) != 0)
558 		goto fail;
559 
560 	if ((error = p_candebug(td, p)) != 0)
561 		goto fail;
562 
563 	/*
564 	 * System processes can't be debugged.
565 	 */
566 	if ((p->p_flag & P_SYSTEM) != 0) {
567 		error = EINVAL;
568 		goto fail;
569 	}
570 
571 	if (tid == 0) {
572 		if ((p->p_flag & P_STOPPED_TRACE) != 0) {
573 			KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
574 			td2 = p->p_xthread;
575 		} else {
576 			td2 = FIRST_THREAD_IN_PROC(p);
577 		}
578 		tid = td2->td_tid;
579 	}
580 
581 #ifdef COMPAT_IA32
582 	/*
583 	 * Test if we're a 32 bit client and what the target is.
584 	 * Set the wrap controls accordingly.
585 	 */
586 	if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) {
587 		if (td2->td_proc->p_sysent == &ia32_freebsd_sysvec)
588 			safe = 1;
589 		wrap32 = 1;
590 	}
591 #endif
592 	/*
593 	 * Permissions check
594 	 */
595 	switch (req) {
596 	case PT_TRACE_ME:
597 		/* Always legal. */
598 		break;
599 
600 	case PT_ATTACH:
601 		/* Self */
602 		if (p->p_pid == td->td_proc->p_pid) {
603 			error = EINVAL;
604 			goto fail;
605 		}
606 
607 		/* Already traced */
608 		if (p->p_flag & P_TRACED) {
609 			error = EBUSY;
610 			goto fail;
611 		}
612 
613 		/* Can't trace an ancestor if you're being traced. */
614 		if (curp->p_flag & P_TRACED) {
615 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
616 				if (pp == p) {
617 					error = EINVAL;
618 					goto fail;
619 				}
620 			}
621 		}
622 
623 
624 		/* OK */
625 		break;
626 
627 	case PT_CLEARSTEP:
628 		/* Allow thread to clear single step for itself */
629 		if (td->td_tid == tid)
630 			break;
631 
632 		/* FALLTHROUGH */
633 	default:
634 		/* not being traced... */
635 		if ((p->p_flag & P_TRACED) == 0) {
636 			error = EPERM;
637 			goto fail;
638 		}
639 
640 		/* not being traced by YOU */
641 		if (p->p_pptr != td->td_proc) {
642 			error = EBUSY;
643 			goto fail;
644 		}
645 
646 		/* not currently stopped */
647 		if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
648 		    p->p_suspcount != p->p_numthreads  ||
649 		    (p->p_flag & P_WAITED) == 0) {
650 			error = EBUSY;
651 			goto fail;
652 		}
653 
654 		if ((p->p_flag & P_STOPPED_TRACE) == 0) {
655 			static int count = 0;
656 			if (count++ == 0)
657 				printf("P_STOPPED_TRACE not set.\n");
658 		}
659 
660 		/* OK */
661 		break;
662 	}
663 
664 	/* Keep this process around until we finish this request. */
665 	_PHOLD(p);
666 
667 #ifdef FIX_SSTEP
668 	/*
669 	 * Single step fixup ala procfs
670 	 */
671 	FIX_SSTEP(td2);
672 #endif
673 
674 	/*
675 	 * Actually do the requests
676 	 */
677 
678 	td->td_retval[0] = 0;
679 
680 	switch (req) {
681 	case PT_TRACE_ME:
682 		/* set my trace flag and "owner" so it can read/write me */
683 		p->p_flag |= P_TRACED;
684 		p->p_oppid = p->p_pptr->p_pid;
685 		break;
686 
687 	case PT_ATTACH:
688 		/* security check done above */
689 		p->p_flag |= P_TRACED;
690 		p->p_oppid = p->p_pptr->p_pid;
691 		if (p->p_pptr != td->td_proc)
692 			proc_reparent(p, td->td_proc);
693 		data = SIGSTOP;
694 		goto sendsig;	/* in PT_CONTINUE below */
695 
696 	case PT_CLEARSTEP:
697 		error = ptrace_clear_single_step(td2);
698 		break;
699 
700 	case PT_SETSTEP:
701 		error = ptrace_single_step(td2);
702 		break;
703 
704 	case PT_SUSPEND:
705 		thread_lock(td2);
706 		td2->td_flags |= TDF_DBSUSPEND;
707 		thread_unlock(td2);
708 		break;
709 
710 	case PT_RESUME:
711 		thread_lock(td2);
712 		td2->td_flags &= ~TDF_DBSUSPEND;
713 		thread_unlock(td2);
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 			thread_lock(td2);
788 			td2->td_flags &= ~TDF_XSIG;
789 			thread_unlock(td2);
790 			td2->td_xsig = data;
791 
792 			PROC_SLOCK(p);
793 			if (req == PT_DETACH) {
794 				struct thread *td3;
795 				FOREACH_THREAD_IN_PROC(p, td3) {
796 					thread_lock(td3);
797 					td3->td_flags &= ~TDF_DBSUSPEND;
798 					thread_unlock(td3);
799 				}
800 			}
801 			/*
802 			 * unsuspend all threads, to not let a thread run,
803 			 * you should use PT_SUSPEND to suspend it before
804 			 * continuing process.
805 			 */
806 #ifdef KSE
807 			PROC_SUNLOCK(p);
808 			thread_continued(p);
809 			PROC_SLOCK(p);
810 #endif
811 			p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
812 			thread_unsuspend(p);
813 			PROC_SUNLOCK(p);
814 		} else {
815 			if (data)
816 				psignal(p, data);
817 		}
818 		break;
819 
820 	case PT_WRITE_I:
821 	case PT_WRITE_D:
822 		write = 1;
823 		/* FALLTHROUGH */
824 	case PT_READ_I:
825 	case PT_READ_D:
826 		PROC_UNLOCK(p);
827 		tmp = 0;
828 		/* write = 0 set above */
829 		iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
830 		iov.iov_len = sizeof(int);
831 		uio.uio_iov = &iov;
832 		uio.uio_iovcnt = 1;
833 		uio.uio_offset = (off_t)(uintptr_t)addr;
834 		uio.uio_resid = sizeof(int);
835 		uio.uio_segflg = UIO_SYSSPACE;	/* i.e.: the uap */
836 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
837 		uio.uio_td = td;
838 		error = proc_rwmem(p, &uio);
839 		if (uio.uio_resid != 0) {
840 			/*
841 			 * XXX proc_rwmem() doesn't currently return ENOSPC,
842 			 * so I think write() can bogusly return 0.
843 			 * XXX what happens for short writes?  We don't want
844 			 * to write partial data.
845 			 * XXX proc_rwmem() returns EPERM for other invalid
846 			 * addresses.  Convert this to EINVAL.  Does this
847 			 * clobber returns of EPERM for other reasons?
848 			 */
849 			if (error == 0 || error == ENOSPC || error == EPERM)
850 				error = EINVAL;	/* EOF */
851 		}
852 		if (!write)
853 			td->td_retval[0] = tmp;
854 		PROC_LOCK(p);
855 		break;
856 
857 	case PT_IO:
858 #ifdef COMPAT_IA32
859 		if (wrap32) {
860 			piod32 = addr;
861 			iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
862 			iov.iov_len = piod32->piod_len;
863 			uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
864 			uio.uio_resid = piod32->piod_len;
865 		} else
866 #endif
867 		{
868 			piod = addr;
869 			iov.iov_base = piod->piod_addr;
870 			iov.iov_len = piod->piod_len;
871 			uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
872 			uio.uio_resid = piod->piod_len;
873 		}
874 		uio.uio_iov = &iov;
875 		uio.uio_iovcnt = 1;
876 		uio.uio_segflg = UIO_USERSPACE;
877 		uio.uio_td = td;
878 #ifdef COMPAT_IA32
879 		tmp = wrap32 ? piod32->piod_op : piod->piod_op;
880 #else
881 		tmp = piod->piod_op;
882 #endif
883 		switch (tmp) {
884 		case PIOD_READ_D:
885 		case PIOD_READ_I:
886 			uio.uio_rw = UIO_READ;
887 			break;
888 		case PIOD_WRITE_D:
889 		case PIOD_WRITE_I:
890 			uio.uio_rw = UIO_WRITE;
891 			break;
892 		default:
893 			error = EINVAL;
894 			goto out;
895 		}
896 		PROC_UNLOCK(p);
897 		error = proc_rwmem(p, &uio);
898 #ifdef COMPAT_IA32
899 		if (wrap32)
900 			piod32->piod_len -= uio.uio_resid;
901 		else
902 #endif
903 			piod->piod_len -= uio.uio_resid;
904 		PROC_LOCK(p);
905 		break;
906 
907 	case PT_KILL:
908 		data = SIGKILL;
909 		goto sendsig;	/* in PT_CONTINUE above */
910 
911 	case PT_SETREGS:
912 		error = PROC_WRITE(regs, td2, addr);
913 		break;
914 
915 	case PT_GETREGS:
916 		error = PROC_READ(regs, td2, addr);
917 		break;
918 
919 	case PT_SETFPREGS:
920 		error = PROC_WRITE(fpregs, td2, addr);
921 		break;
922 
923 	case PT_GETFPREGS:
924 		error = PROC_READ(fpregs, td2, addr);
925 		break;
926 
927 	case PT_SETDBREGS:
928 		error = PROC_WRITE(dbregs, td2, addr);
929 		break;
930 
931 	case PT_GETDBREGS:
932 		error = PROC_READ(dbregs, td2, addr);
933 		break;
934 
935 	case PT_LWPINFO:
936 		if (data <= 0 || data > sizeof(*pl)) {
937 			error = EINVAL;
938 			break;
939 		}
940 		pl = addr;
941 		pl->pl_lwpid = td2->td_tid;
942 		if (td2->td_flags & TDF_XSIG)
943 			pl->pl_event = PL_EVENT_SIGNAL;
944 		else
945 			pl->pl_event = 0;
946 #ifdef KSE
947 		if (td2->td_pflags & TDP_SA) {
948 			pl->pl_flags = PL_FLAG_SA;
949 			if (td2->td_upcall && !TD_CAN_UNBIND(td2))
950 				pl->pl_flags |= PL_FLAG_BOUND;
951 		} else {
952 			pl->pl_flags = 0;
953 		}
954 #else
955 		pl->pl_flags = 0;
956 #endif
957 		pl->pl_sigmask = td2->td_sigmask;
958 		pl->pl_siglist = td2->td_siglist;
959 		break;
960 
961 	case PT_GETNUMLWPS:
962 		td->td_retval[0] = p->p_numthreads;
963 		break;
964 
965 	case PT_GETLWPLIST:
966 		if (data <= 0) {
967 			error = EINVAL;
968 			break;
969 		}
970 		num = imin(p->p_numthreads, data);
971 		PROC_UNLOCK(p);
972 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
973 		tmp = 0;
974 		PROC_LOCK(p);
975 		PROC_SLOCK(p);
976 		FOREACH_THREAD_IN_PROC(p, td2) {
977 			if (tmp >= num)
978 				break;
979 			buf[tmp++] = td2->td_tid;
980 		}
981 		PROC_SUNLOCK(p);
982 		PROC_UNLOCK(p);
983 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
984 		free(buf, M_TEMP);
985 		if (!error)
986 			td->td_retval[0] = tmp;
987 		PROC_LOCK(p);
988 		break;
989 
990 	default:
991 #ifdef __HAVE_PTRACE_MACHDEP
992 		if (req >= PT_FIRSTMACH) {
993 			PROC_UNLOCK(p);
994 			error = cpu_ptrace(td2, req, addr, data);
995 			PROC_LOCK(p);
996 		} else
997 #endif
998 			/* Unknown request. */
999 			error = EINVAL;
1000 		break;
1001 	}
1002 
1003 out:
1004 	/* Drop our hold on this process now that the request has completed. */
1005 	_PRELE(p);
1006 fail:
1007 	PROC_UNLOCK(p);
1008 	if (proctree_locked)
1009 		sx_xunlock(&proctree_lock);
1010 	return (error);
1011 }
1012 #undef PROC_READ
1013 #undef PROC_WRITE
1014 
1015 /*
1016  * Stop a process because of a debugging event;
1017  * stay stopped until p->p_step is cleared
1018  * (cleared by PIOCCONT in procfs).
1019  */
1020 void
1021 stopevent(struct proc *p, unsigned int event, unsigned int val)
1022 {
1023 
1024 	PROC_LOCK_ASSERT(p, MA_OWNED);
1025 	p->p_step = 1;
1026 	do {
1027 		p->p_xstat = val;
1028 		p->p_xthread = NULL;
1029 		p->p_stype = event;	/* Which event caused the stop? */
1030 		wakeup(&p->p_stype);	/* Wake up any PIOCWAIT'ing procs */
1031 		msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1032 	} while (p->p_step);
1033 }
1034