xref: /netbsd-src/sys/kern/sys_process.c (revision de2138164cd16145ee5fd4d0aef1e8f952c1a9fb)
1 /*	$NetBSD: sys_process.c,v 1.121 2007/02/17 22:31:44 pavel Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * This code is derived from software contributed to Berkeley by
13  * Jan-Simon Pendry.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
40  */
41 
42 /*-
43  * Copyright (c) 1993 Jan-Simon Pendry.
44  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
45  *
46  * This code is derived from software contributed to Berkeley by
47  * Jan-Simon Pendry.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  * 3. All advertising materials mentioning features or use of this software
58  *    must display the following acknowledgement:
59  *	This product includes software developed by the University of
60  *	California, Berkeley and its contributors.
61  * 4. Neither the name of the University nor the names of its contributors
62  *    may be used to endorse or promote products derived from this software
63  *    without specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75  * SUCH DAMAGE.
76  *
77  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
78  */
79 
80 /*
81  * References:
82  *	(1) Bach's "The Design of the UNIX Operating System",
83  *	(2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
84  *	(3) the "4.4BSD Programmer's Reference Manual" published
85  *		by USENIX and O'Reilly & Associates.
86  * The 4.4BSD PRM does a reasonably good job of documenting what the various
87  * ptrace() requests should actually do, and its text is quoted several times
88  * in this file.
89  */
90 
91 #include "opt_coredump.h"
92 #include "opt_ptrace.h"
93 #include "opt_ktrace.h"
94 
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.121 2007/02/17 22:31:44 pavel Exp $");
97 
98 #include <sys/param.h>
99 #include <sys/systm.h>
100 #include <sys/proc.h>
101 #include <sys/errno.h>
102 #include <sys/ptrace.h>
103 #include <sys/uio.h>
104 #include <sys/user.h>
105 #include <sys/ras.h>
106 #include <sys/malloc.h>
107 #include <sys/kauth.h>
108 #include <sys/mount.h>
109 #include <sys/syscallargs.h>
110 
111 #include <uvm/uvm_extern.h>
112 
113 #include <machine/reg.h>
114 
115 #ifdef PTRACE
116 /*
117  * Process debugging system call.
118  */
119 int
120 sys_ptrace(struct lwp *l, void *v, register_t *retval)
121 {
122 	struct sys_ptrace_args /* {
123 		syscallarg(int) req;
124 		syscallarg(pid_t) pid;
125 		syscallarg(caddr_t) addr;
126 		syscallarg(int) data;
127 	} */ *uap = v;
128 	struct proc *p = l->l_proc;
129 	struct lwp *lt;
130 	struct proc *t;				/* target process */
131 	struct uio uio;
132 	struct iovec iov;
133 	struct ptrace_io_desc piod;
134 	struct ptrace_lwpinfo pl;
135 	struct vmspace *vm;
136 	int error, write, tmp, req, pheld;
137 	ksiginfo_t ksi;
138 #ifdef COREDUMP
139 	char *path;
140 #endif
141 
142 	error = 0;
143 	req = SCARG(uap, req);
144 
145 	/*
146 	 * If attaching or detaching, we need to get a write hold on the
147 	 * proclist lock so that we can re-parent the target process.
148 	 */
149 	rw_enter(&proclist_lock, RW_WRITER);
150 
151 	/* "A foolish consistency..." XXX */
152 	if (req == PT_TRACE_ME)
153 		t = p;
154 	else {
155 		/* Find the process we're supposed to be operating on. */
156 		if ((t = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
157 			rw_exit(&proclist_lock);
158 			return (ESRCH);
159 		}
160 
161 		/* XXX elad - this should be in pfind(). */
162 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
163 		    t, NULL, NULL, NULL);
164 		if (error) {
165 			rw_exit(&proclist_lock);
166 			return (ESRCH);
167 		}
168 	}
169 
170 	/*
171 	 * Grab a reference on the process to prevent it from execing or
172 	 * exiting.
173 	 */
174 	mutex_enter(&t->p_mutex);
175 	error = proc_addref(t);
176 	if (error != 0) {
177 		mutex_exit(&t->p_mutex);
178 		rw_exit(&proclist_lock);
179 		return error;
180 	}
181 
182 	/* Make sure we can operate on it. */
183 	switch (req) {
184 	case  PT_TRACE_ME:
185 		/* Saying that you're being traced is always legal. */
186 		break;
187 
188 	case  PT_ATTACH:
189 		/*
190 		 * You can't attach to a process if:
191 		 *	(1) it's the process that's doing the attaching,
192 		 */
193 		if (t->p_pid == p->p_pid) {
194 			error = EINVAL;
195 			break;
196 		}
197 
198 		/*
199 		 *  (2) it's a system process
200 		 */
201 		if (t->p_flag & PK_SYSTEM) {
202 			error = EPERM;
203 			break;
204 		}
205 
206 		/*
207 		 *	(3) it's already being traced, or
208 		 */
209 		if (ISSET(t->p_slflag, PSL_TRACED)) {
210 			error = EBUSY;
211 			break;
212 		}
213 
214 		/*
215 		 * 	(4) the tracer is chrooted, and its root directory is
216 		 * 	    not at or above the root directory of the tracee
217 		 */
218 		mutex_exit(&t->p_mutex);	/* XXXSMP */
219 		tmp = proc_isunder(t, l);
220 		mutex_enter(&t->p_mutex);	/* XXXSMP */
221 		if (!tmp) {
222 			error = EPERM;
223 			break;
224 		}
225 		break;
226 
227 	case  PT_READ_I:
228 	case  PT_READ_D:
229 	case  PT_WRITE_I:
230 	case  PT_WRITE_D:
231 	case  PT_IO:
232 #ifdef PT_GETREGS
233 	case  PT_GETREGS:
234 #endif
235 #ifdef PT_SETREGS
236 	case  PT_SETREGS:
237 #endif
238 #ifdef PT_GETFPREGS
239 	case  PT_GETFPREGS:
240 #endif
241 #ifdef PT_SETFPREGS
242 	case  PT_SETFPREGS:
243 #endif
244 #ifdef __HAVE_PTRACE_MACHDEP
245 	PTRACE_MACHDEP_REQUEST_CASES
246 #endif
247 		/*
248 		 * You can't read/write the memory or registers of a process
249 		 * if the tracer is chrooted, and its root directory is not at
250 		 * or above the root directory of the tracee.
251 		 */
252 		mutex_exit(&t->p_mutex);	/* XXXSMP */
253 		tmp = proc_isunder(t, l);
254 		mutex_enter(&t->p_mutex);	/* XXXSMP */
255 		if (!tmp) {
256 			error = EPERM;
257 			break;
258 		}
259 		/*FALLTHROUGH*/
260 
261 	case  PT_CONTINUE:
262 	case  PT_KILL:
263 	case  PT_DETACH:
264 	case  PT_LWPINFO:
265 	case  PT_SYSCALL:
266 #ifdef COREDUMP
267 	case  PT_DUMPCORE:
268 #endif
269 #ifdef PT_STEP
270 	case  PT_STEP:
271 #endif
272 		/*
273 		 * You can't do what you want to the process if:
274 		 *	(1) It's not being traced at all,
275 		 */
276 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
277 			error = EPERM;
278 			break;
279 		}
280 
281 		/*
282 		 *	(2) it's being traced by procfs (which has
283 		 *	    different signal delivery semantics),
284 		 */
285 		if (ISSET(t->p_slflag, PSL_FSTRACE)) {
286 			uprintf("file system traced\n");
287 			error = EBUSY;
288 			break;
289 		}
290 
291 		/*
292 		 *	(3) it's not being traced by _you_, or
293 		 */
294 		if (t->p_pptr != p) {
295 			uprintf("parent %d != %d\n", t->p_pptr->p_pid, p->p_pid);
296 			error = EBUSY;
297 			break;
298 		}
299 
300 		/*
301 		 *	(4) it's not currently stopped.
302 		 */
303 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
304 			uprintf("stat %d flag %d\n", t->p_stat,
305 			    !t->p_waited);
306 			error = EBUSY;
307 			break;
308 		}
309 		break;
310 
311 	default:			/* It was not a legal request. */
312 		error = EINVAL;
313 		break;
314 	}
315 
316 	if (error == 0)
317 		error = kauth_authorize_process(l->l_cred,
318 		    KAUTH_PROCESS_CANPTRACE, t, KAUTH_ARG(req),
319 		    NULL, NULL);
320 
321 	if (error != 0) {
322 		rw_exit(&proclist_lock);
323 		proc_delref(t);
324 		mutex_exit(&t->p_mutex);
325 		return error;
326 	}
327 
328 	/*
329 	 * Which locks do we need held? XXX Ugly.
330 	 */
331 	switch (req) {
332 #ifdef PT_STEP
333 	case PT_STEP:
334 #endif
335 	case PT_CONTINUE:
336 	case PT_DETACH:
337 	case PT_KILL:
338 	case PT_SYSCALL:
339 	case PT_ATTACH:
340 		pheld = 1;
341 		break;
342 	default:
343 		rw_exit(&proclist_lock);
344 		mutex_exit(&t->p_mutex);
345 		pheld = 0;
346 		break;
347 	}
348 
349 
350 	/* Do single-step fixup if needed. */
351 	FIX_SSTEP(t);
352 
353 	/*
354 	 * XXX NJWLWP
355 	 *
356 	 * The entire ptrace interface needs work to be useful to a
357 	 * process with multiple LWPs. For the moment, we'll kluge
358 	 * this; memory access will be fine, but register access will
359 	 * be weird.
360 	 */
361 	mutex_enter(&t->p_smutex);
362 	lt = proc_representative_lwp(t, NULL, 1);
363 	lwp_addref(lt);
364 	mutex_exit(&t->p_smutex);
365 
366 	/* Now do the operation. */
367 	write = 0;
368 	*retval = 0;
369 	tmp = 0;
370 
371 	switch (req) {
372 	case  PT_TRACE_ME:
373 		/* Just set the trace flag. */
374 		mutex_enter(&t->p_smutex);
375 		SET(t->p_slflag, PSL_TRACED);
376 		mutex_exit(&t->p_smutex);
377 		t->p_opptr = t->p_pptr;
378 		break;
379 
380 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
381 	case  PT_WRITE_D:
382 #if defined(__HAVE_RAS)
383 		/*
384 		 * Can't write to a RAS
385 		 */
386 		if (ras_lookup(t, SCARG(uap, addr)) != (caddr_t)-1) {
387 			error = EACCES;
388 			break;
389 		}
390 #endif
391 		write = 1;
392 		tmp = SCARG(uap, data);
393 		/* FALLTHROUGH */
394 
395 	case  PT_READ_I:		/* XXX no separate I and D spaces */
396 	case  PT_READ_D:
397 		/* write = 0 done above. */
398 		iov.iov_base = (caddr_t)&tmp;
399 		iov.iov_len = sizeof(tmp);
400 		uio.uio_iov = &iov;
401 		uio.uio_iovcnt = 1;
402 		uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr);
403 		uio.uio_resid = sizeof(tmp);
404 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
405 		UIO_SETUP_SYSSPACE(&uio);
406 
407 		error = process_domem(l, lt, &uio);
408 		if (!write)
409 			*retval = tmp;
410 		break;
411 
412 	case  PT_IO:
413 		error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
414 		if (error)
415 			break;
416 		switch (piod.piod_op) {
417 		case PIOD_READ_D:
418 		case PIOD_READ_I:
419 			uio.uio_rw = UIO_READ;
420 			break;
421 		case PIOD_WRITE_D:
422 		case PIOD_WRITE_I:
423 #if defined(__HAVE_RAS)
424 			/*
425 			 * Can't write to a RAS
426 			 */
427 			if (!LIST_EMPTY(&t->p_raslist) &&
428 			    (ras_lookup(t, SCARG(uap, addr)) != (caddr_t)-1)) {
429 				return (EACCES);
430 			}
431 #endif
432 			uio.uio_rw = UIO_WRITE;
433 			break;
434 		default:
435 			error = EINVAL;
436 			break;
437 		}
438 		if (error)
439 			break;
440 		error = proc_vmspace_getref(l->l_proc, &vm);
441 		if (error)
442 			break;
443 		iov.iov_base = piod.piod_addr;
444 		iov.iov_len = piod.piod_len;
445 		uio.uio_iov = &iov;
446 		uio.uio_iovcnt = 1;
447 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
448 		uio.uio_resid = piod.piod_len;
449 		uio.uio_vmspace = vm;
450 
451 		error = process_domem(l, lt, &uio);
452 		piod.piod_len -= uio.uio_resid;
453 		(void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
454 		uvmspace_free(vm);
455 		break;
456 
457 #ifdef COREDUMP
458 	case  PT_DUMPCORE:
459 		if ((path = SCARG(uap, addr)) != NULL) {
460 			char *dst;
461 			int len = SCARG(uap, data);
462 			if (len < 0 || len >= MAXPATHLEN) {
463 				error = EINVAL;
464 				break;
465 			}
466 			dst = malloc(len + 1, M_TEMP, M_WAITOK);
467 			if ((error = copyin(path, dst, len)) != 0) {
468 				free(dst, M_TEMP);
469 				break;
470 			}
471 			path = dst;
472 			path[len] = '\0';
473 		}
474 		error = coredump(lt, path);
475 		if (path)
476 			free(path, M_TEMP);
477 		break;
478 #endif
479 
480 #ifdef PT_STEP
481 	case  PT_STEP:
482 		/*
483 		 * From the 4.4BSD PRM:
484 		 * "Execution continues as in request PT_CONTINUE; however
485 		 * as soon as possible after execution of at least one
486 		 * instruction, execution stops again. [ ... ]"
487 		 */
488 #endif
489 	case  PT_CONTINUE:
490 	case  PT_SYSCALL:
491 	case  PT_DETACH:
492 		mutex_enter(&t->p_smutex);
493 		if (req == PT_SYSCALL) {
494 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
495 				SET(t->p_slflag, PSL_SYSCALL);
496 #ifdef __HAVE_SYSCALL_INTERN
497 				(*t->p_emul->e_syscall_intern)(t);
498 #endif
499 			}
500 		} else {
501 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
502 				CLR(t->p_slflag, PSL_SYSCALL);
503 #ifdef __HAVE_SYSCALL_INTERN
504 				(*t->p_emul->e_syscall_intern)(t);
505 #endif
506 			}
507 		}
508 		mutex_exit(&t->p_smutex);
509 
510 		/*
511 		 * From the 4.4BSD PRM:
512 		 * "The data argument is taken as a signal number and the
513 		 * child's execution continues at location addr as if it
514 		 * incurred that signal.  Normally the signal number will
515 		 * be either 0 to indicate that the signal that caused the
516 		 * stop should be ignored, or that value fetched out of
517 		 * the process's image indicating which signal caused
518 		 * the stop.  If addr is (int *)1 then execution continues
519 		 * from where it stopped."
520 		 */
521 
522 		/* Check that the data is a valid signal number or zero. */
523 		if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) {
524 			error = EINVAL;
525 			break;
526 		}
527 
528 		PHOLD(lt);
529 
530 		/* If the address parameter is not (int *)1, set the pc. */
531 		if ((int *)SCARG(uap, addr) != (int *)1)
532 			if ((error = process_set_pc(lt, SCARG(uap, addr))) != 0) {
533 				PRELE(lt);
534 				break;
535 			}
536 
537 #ifdef PT_STEP
538 		/*
539 		 * Arrange for a single-step, if that's requested and possible.
540 		 */
541 		error = process_sstep(lt, req == PT_STEP);
542 		if (error) {
543 			PRELE(lt);
544 			break;
545 		}
546 #endif
547 
548 		PRELE(lt);
549 
550 		if (req == PT_DETACH) {
551 			mutex_enter(&t->p_smutex);
552 			CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
553 			mutex_exit(&t->p_smutex);
554 
555 			/* give process back to original parent or init */
556 			if (t->p_opptr != t->p_pptr) {
557 				struct proc *pp = t->p_opptr;
558 				proc_reparent(t, pp ? pp : initproc);
559 			}
560 
561 			/* not being traced any more */
562 			t->p_opptr = NULL;
563 		}
564 
565 	sendsig:
566 		/* Finally, deliver the requested signal (or none). */
567 		mutex_enter(&proclist_mutex);
568 		mutex_enter(&t->p_smutex);
569 		if (t->p_stat == SSTOP) {
570 			/*
571 			 * Unstop the process.  If it needs to take a
572 			 * signal, make all efforts to ensure that at
573 			 * an LWP runs to see it.
574 			 */
575 			t->p_xstat = SCARG(uap, data);
576 			proc_unstop(t);
577 		} else if (SCARG(uap, data) != 0) {
578 			KSI_INIT_EMPTY(&ksi);
579 			ksi.ksi_signo = SCARG(uap, data);
580 			kpsignal2(t, &ksi);
581 		}
582 		mutex_exit(&t->p_smutex);
583 		mutex_exit(&proclist_mutex);
584 		break;
585 
586 	case  PT_KILL:
587 		/* just send the process a KILL signal. */
588 		SCARG(uap, data) = SIGKILL;
589 		goto sendsig;	/* in PT_CONTINUE, above. */
590 
591 	case  PT_ATTACH:
592 		/*
593 		 * Go ahead and set the trace flag.
594 		 * Save the old parent (it's reset in
595 		 *   _DETACH, and also in kern_exit.c:wait4()
596 		 * Reparent the process so that the tracing
597 		 *   proc gets to see all the action.
598 		 * Stop the target.
599 		 */
600 		t->p_opptr = t->p_pptr;
601 		if (t->p_pptr != p) {
602 			mutex_enter(&t->p_pptr->p_smutex);
603 			t->p_pptr->p_slflag |= PSL_CHTRACED;
604 			mutex_exit(&t->p_pptr->p_smutex);
605 			proc_reparent(t, p);
606 		}
607 		mutex_enter(&t->p_smutex);
608 		SET(t->p_slflag, PSL_TRACED);
609 		mutex_exit(&t->p_smutex);
610 		SCARG(uap, data) = SIGSTOP;
611 		goto sendsig;
612 
613 	case PT_LWPINFO:
614 		if (SCARG(uap, data) != sizeof(pl)) {
615 			error = EINVAL;
616 			break;
617 		}
618 		error = copyin(SCARG(uap, addr), &pl, sizeof(pl));
619 		if (error)
620 			break;
621 		tmp = pl.pl_lwpid;
622 		lwp_delref(lt);
623 		mutex_enter(&t->p_smutex);
624 		if (tmp == 0)
625 			lt = LIST_FIRST(&t->p_lwps);
626 		else {
627 			lt = lwp_find(p, tmp);
628 			if (lt == NULL) {
629 				mutex_exit(&t->p_smutex);
630 				error = ESRCH;
631 				break;
632 			}
633 		}
634 		pl.pl_lwpid = 0;
635 		pl.pl_event = 0;
636 		if (lt) {
637 			pl.pl_lwpid = lt->l_lid;
638 			if (lt->l_lid == t->p_sigctx.ps_lwp)
639 				pl.pl_event = PL_EVENT_SIGNAL;
640 		}
641 		mutex_exit(&t->p_smutex);
642 
643 		error = copyout(&pl, SCARG(uap, addr), sizeof(pl));
644 		break;
645 
646 #ifdef PT_SETREGS
647 	case  PT_SETREGS:
648 		write = 1;
649 #endif
650 #ifdef PT_GETREGS
651 	case  PT_GETREGS:
652 		/* write = 0 done above. */
653 #endif
654 #if defined(PT_SETREGS) || defined(PT_GETREGS)
655 		tmp = SCARG(uap, data);
656 		if (tmp != 0 && t->p_nlwps > 1) {
657 			lwp_delref(lt);
658 			mutex_enter(&t->p_smutex);
659 			lt = lwp_find(t, tmp);
660 			if (lt == NULL) {
661 				mutex_exit(&t->p_smutex);
662 				error = ESRCH;
663 				break;
664 			}
665 			lwp_addref(lt);
666 			mutex_exit(&t->p_smutex);
667 		}
668 		if (!process_validregs(lt))
669 			error = EINVAL;
670 		else {
671 			error = proc_vmspace_getref(l->l_proc, &vm);
672 			if (error)
673 				break;
674 			iov.iov_base = SCARG(uap, addr);
675 			iov.iov_len = sizeof(struct reg);
676 			uio.uio_iov = &iov;
677 			uio.uio_iovcnt = 1;
678 			uio.uio_offset = 0;
679 			uio.uio_resid = sizeof(struct reg);
680 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
681 			uio.uio_vmspace = vm;
682 
683 			error = process_doregs(l, lt, &uio);
684 			uvmspace_free(vm);
685 		}
686 		break;
687 #endif
688 
689 #ifdef PT_SETFPREGS
690 	case  PT_SETFPREGS:
691 		write = 1;
692 #endif
693 #ifdef PT_GETFPREGS
694 	case  PT_GETFPREGS:
695 		/* write = 0 done above. */
696 #endif
697 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
698 		tmp = SCARG(uap, data);
699 		if (tmp != 0 && t->p_nlwps > 1) {
700 			lwp_delref(lt);
701 			mutex_enter(&t->p_smutex);
702 			lt = lwp_find(t, tmp);
703 			if (lt == NULL) {
704 				mutex_exit(&t->p_smutex);
705 				error = ESRCH;
706 				break;
707 			}
708 			lwp_addref(lt);
709 			mutex_exit(&t->p_smutex);
710 		}
711 		if (!process_validfpregs(lt))
712 			error = EINVAL;
713 		else {
714 			error = proc_vmspace_getref(l->l_proc, &vm);
715 			if (error)
716 				break;
717 			iov.iov_base = SCARG(uap, addr);
718 			iov.iov_len = sizeof(struct fpreg);
719 			uio.uio_iov = &iov;
720 			uio.uio_iovcnt = 1;
721 			uio.uio_offset = 0;
722 			uio.uio_resid = sizeof(struct fpreg);
723 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
724 			uio.uio_vmspace = vm;
725 
726 			error = process_dofpregs(l, lt, &uio);
727 			uvmspace_free(vm);
728 		}
729 		break;
730 #endif
731 
732 #ifdef __HAVE_PTRACE_MACHDEP
733 	PTRACE_MACHDEP_REQUEST_CASES
734 		error = ptrace_machdep_dorequest(l, lt,
735 		    req, SCARG(uap, addr), SCARG(uap, data));
736 		break;
737 #endif
738 	}
739 
740 	if (lt != NULL)
741 		lwp_delref(lt);
742 
743 	if (pheld) {
744 		proc_delref(t);
745 		mutex_exit(&t->p_mutex);
746 		rw_exit(&proclist_lock);
747 	} else {
748 		mutex_enter(&t->p_mutex);
749 		proc_delref(t);
750 		mutex_exit(&t->p_mutex);
751 	}
752 
753 	return error;
754 }
755 
756 int
757 process_doregs(struct lwp *curl /*tracer*/,
758     struct lwp *l /*traced*/,
759     struct uio *uio)
760 {
761 #if defined(PT_GETREGS) || defined(PT_SETREGS)
762 	int error;
763 	struct reg r;
764 	char *kv;
765 	int kl;
766 
767 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
768 		return EINVAL;
769 
770 	kl = sizeof(r);
771 	kv = (char *)&r;
772 
773 	kv += uio->uio_offset;
774 	kl -= uio->uio_offset;
775 	if ((size_t)kl > uio->uio_resid)
776 		kl = uio->uio_resid;
777 
778 	PHOLD(l);
779 
780 	error = process_read_regs(l, &r);
781 	if (error == 0)
782 		error = uiomove(kv, kl, uio);
783 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
784 		if (l->l_stat != LSSTOP)
785 			error = EBUSY;
786 		else
787 			error = process_write_regs(l, &r);
788 	}
789 
790 	PRELE(l);
791 
792 	uio->uio_offset = 0;
793 	return (error);
794 #else
795 	return (EINVAL);
796 #endif
797 }
798 
799 int
800 process_validregs(struct lwp *l)
801 {
802 
803 #if defined(PT_SETREGS) || defined(PT_GETREGS)
804 	return ((l->l_flag & LW_SYSTEM) == 0);
805 #else
806 	return (0);
807 #endif
808 }
809 
810 int
811 process_dofpregs(struct lwp *curl /*tracer*/,
812     struct lwp *l /*traced*/,
813     struct uio *uio)
814 {
815 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
816 	int error;
817 	struct fpreg r;
818 	char *kv;
819 	int kl;
820 
821 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
822 		return EINVAL;
823 
824 	kl = sizeof(r);
825 	kv = (char *)&r;
826 
827 	kv += uio->uio_offset;
828 	kl -= uio->uio_offset;
829 	if ((size_t)kl > uio->uio_resid)
830 		kl = uio->uio_resid;
831 
832 	PHOLD(l);
833 
834 	error = process_read_fpregs(l, &r);
835 	if (error == 0)
836 		error = uiomove(kv, kl, uio);
837 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
838 		if (l->l_stat != LSSTOP)
839 			error = EBUSY;
840 		else
841 			error = process_write_fpregs(l, &r);
842 	}
843 
844 	PRELE(l);
845 
846 	uio->uio_offset = 0;
847 	return (error);
848 #else
849 	return (EINVAL);
850 #endif
851 }
852 
853 int
854 process_validfpregs(struct lwp *l)
855 {
856 
857 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
858 	return ((l->l_flag & LW_SYSTEM) == 0);
859 #else
860 	return (0);
861 #endif
862 }
863 #endif /* PTRACE */
864 
865 #if defined(KTRACE) || defined(PTRACE) || defined(SYSTRACE)
866 int
867 process_domem(struct lwp *curl /*tracer*/,
868     struct lwp *l /*traced*/,
869     struct uio *uio)
870 {
871 	struct proc *p = l->l_proc;	/* traced */
872 	struct vmspace *vm;
873 	int error;
874 
875 	size_t len;
876 #ifdef PMAP_NEED_PROCWR
877 	vaddr_t	addr;
878 #endif
879 
880 	error = 0;
881 	len = uio->uio_resid;
882 
883 	if (len == 0)
884 		return (0);
885 
886 #ifdef PMAP_NEED_PROCWR
887 	addr = uio->uio_offset;
888 #endif
889 
890 	vm = p->p_vmspace;
891 
892 	simple_lock(&vm->vm_map.ref_lock);
893 	if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1)
894 		error = EFAULT;
895 	if (error == 0)
896 		p->p_vmspace->vm_refcnt++;  /* XXX */
897 	simple_unlock(&vm->vm_map.ref_lock);
898 	if (error != 0)
899 		return (error);
900 	error = uvm_io(&vm->vm_map, uio);
901 	uvmspace_free(vm);
902 
903 #ifdef PMAP_NEED_PROCWR
904 	if (error == 0 && uio->uio_rw == UIO_WRITE)
905 		pmap_procwr(p, addr, len);
906 #endif
907 	return (error);
908 }
909 #endif /* KTRACE || PTRACE || SYSTRACE */
910 
911 #if defined(KTRACE) || defined(PTRACE)
912 void
913 process_stoptrace(struct lwp *l)
914 {
915 	struct proc *p = l->l_proc, *pp;
916 
917 	mutex_enter(&proclist_mutex);
918 	mutex_enter(&p->p_smutex);
919 	pp = p->p_pptr;
920 	if (pp->p_pid == 1) {
921 		CLR(p->p_slflag, PSL_SYSCALL);	/* XXXSMP */
922 		mutex_exit(&p->p_smutex);
923 		mutex_exit(&proclist_mutex);
924 		return;
925 	}
926 
927 	p->p_xstat = SIGTRAP;
928 	proc_stop(p, 1, SIGSTOP);
929 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
930 	mutex_exit(&p->p_smutex);
931 	mutex_exit(&proclist_mutex);
932 	lwp_lock(l);
933 	mi_switch(l, NULL);
934 	KERNEL_LOCK(l->l_biglocks, l);
935 }
936 #endif	/* KTRACE || PTRACE */
937