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