xref: /netbsd-src/sys/kern/sys_process.c (revision 7c3f385475147b6e1c4753f2bee961630e2dfc40)
1 /*	$NetBSD: sys_process.c,v 1.136 2008/02/24 18:30:07 dsl 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 <sys/cdefs.h>
92 __KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.136 2008/02/24 18:30:07 dsl Exp $");
93 
94 #include "opt_coredump.h"
95 #include "opt_ptrace.h"
96 #include "opt_ktrace.h"
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, const struct sys_ptrace_args *uap, register_t *retval)
121 {
122 	/* {
123 		syscallarg(int) req;
124 		syscallarg(pid_t) pid;
125 		syscallarg(void *) addr;
126 		syscallarg(int) data;
127 	} */
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 	int signo;
138 	ksiginfo_t ksi;
139 #ifdef COREDUMP
140 	char *path;
141 #endif
142 
143 	error = 0;
144 	req = SCARG(uap, req);
145 
146 	/*
147 	 * If attaching or detaching, we need to get a write hold on the
148 	 * proclist lock so that we can re-parent the target process.
149 	 */
150 	mutex_enter(&proclist_lock);
151 
152 	/* "A foolish consistency..." XXX */
153 	if (req == PT_TRACE_ME) {
154 		t = p;
155 		mutex_enter(&t->p_mutex);
156 	} else {
157 		/* Find the process we're supposed to be operating on. */
158 		if ((t = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
159 			mutex_exit(&proclist_lock);
160 			return (ESRCH);
161 		}
162 
163 		/* XXX-elad */
164 		mutex_enter(&t->p_mutex);
165 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
166 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
167 		if (error) {
168 			mutex_exit(&proclist_lock);
169 			mutex_exit(&t->p_mutex);
170 			return (ESRCH);
171 		}
172 	}
173 
174 	/*
175 	 * Grab a reference on the process to prevent it from execing or
176 	 * exiting.
177 	 */
178 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
179 		mutex_exit(&proclist_lock);
180 		mutex_exit(&t->p_mutex);
181 		return EBUSY;
182 	}
183 
184 	/* Make sure we can operate on it. */
185 	switch (req) {
186 	case  PT_TRACE_ME:
187 		/* Saying that you're being traced is always legal. */
188 		break;
189 
190 	case  PT_ATTACH:
191 		/*
192 		 * You can't attach to a process if:
193 		 *	(1) it's the process that's doing the attaching,
194 		 */
195 		if (t->p_pid == p->p_pid) {
196 			error = EINVAL;
197 			break;
198 		}
199 
200 		/*
201 		 *  (2) it's a system process
202 		 */
203 		if (t->p_flag & PK_SYSTEM) {
204 			error = EPERM;
205 			break;
206 		}
207 
208 		/*
209 		 *	(3) it's already being traced, or
210 		 */
211 		if (ISSET(t->p_slflag, PSL_TRACED)) {
212 			error = EBUSY;
213 			break;
214 		}
215 
216 		/*
217 		 * 	(4) the tracer is chrooted, and its root directory is
218 		 * 	    not at or above the root directory of the tracee
219 		 */
220 		mutex_exit(&t->p_mutex);	/* XXXSMP */
221 		tmp = proc_isunder(t, l);
222 		mutex_enter(&t->p_mutex);	/* XXXSMP */
223 		if (!tmp) {
224 			error = EPERM;
225 			break;
226 		}
227 		break;
228 
229 	case  PT_READ_I:
230 	case  PT_READ_D:
231 	case  PT_WRITE_I:
232 	case  PT_WRITE_D:
233 	case  PT_IO:
234 #ifdef PT_GETREGS
235 	case  PT_GETREGS:
236 #endif
237 #ifdef PT_SETREGS
238 	case  PT_SETREGS:
239 #endif
240 #ifdef PT_GETFPREGS
241 	case  PT_GETFPREGS:
242 #endif
243 #ifdef PT_SETFPREGS
244 	case  PT_SETFPREGS:
245 #endif
246 #ifdef __HAVE_PTRACE_MACHDEP
247 	PTRACE_MACHDEP_REQUEST_CASES
248 #endif
249 		/*
250 		 * You can't read/write the memory or registers of a process
251 		 * if the tracer is chrooted, and its root directory is not at
252 		 * or above the root directory of the tracee.
253 		 */
254 		mutex_exit(&t->p_mutex);	/* XXXSMP */
255 		tmp = proc_isunder(t, l);
256 		mutex_enter(&t->p_mutex);	/* XXXSMP */
257 		if (!tmp) {
258 			error = EPERM;
259 			break;
260 		}
261 		/*FALLTHROUGH*/
262 
263 	case  PT_CONTINUE:
264 	case  PT_KILL:
265 	case  PT_DETACH:
266 	case  PT_LWPINFO:
267 	case  PT_SYSCALL:
268 #ifdef COREDUMP
269 	case  PT_DUMPCORE:
270 #endif
271 #ifdef PT_STEP
272 	case  PT_STEP:
273 #endif
274 		/*
275 		 * You can't do what you want to the process if:
276 		 *	(1) It's not being traced at all,
277 		 */
278 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
279 			error = EPERM;
280 			break;
281 		}
282 
283 		/*
284 		 *	(2) it's being traced by procfs (which has
285 		 *	    different signal delivery semantics),
286 		 */
287 		if (ISSET(t->p_slflag, PSL_FSTRACE)) {
288 			uprintf("file system traced\n");
289 			error = EBUSY;
290 			break;
291 		}
292 
293 		/*
294 		 *	(3) it's not being traced by _you_, or
295 		 */
296 		if (t->p_pptr != p) {
297 			uprintf("parent %d != %d\n", t->p_pptr->p_pid, p->p_pid);
298 			error = EBUSY;
299 			break;
300 		}
301 
302 		/*
303 		 *	(4) it's not currently stopped.
304 		 */
305 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
306 			uprintf("stat %d flag %d\n", t->p_stat,
307 			    !t->p_waited);
308 			error = EBUSY;
309 			break;
310 		}
311 		break;
312 
313 	default:			/* It was not a legal request. */
314 		error = EINVAL;
315 		break;
316 	}
317 
318 	if (error == 0)
319 		error = kauth_authorize_process(l->l_cred,
320 		    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
321 		    NULL, NULL);
322 
323 	if (error != 0) {
324 		mutex_exit(&proclist_lock);
325 		mutex_exit(&t->p_mutex);
326 		rw_exit(&t->p_reflock);
327 		return error;
328 	}
329 
330 	/*
331 	 * Which locks do we need held? XXX Ugly.
332 	 */
333 	switch (req) {
334 #ifdef PT_STEP
335 	case PT_STEP:
336 #endif
337 	case PT_CONTINUE:
338 	case PT_DETACH:
339 	case PT_KILL:
340 	case PT_SYSCALL:
341 	case PT_ATTACH:
342 		pheld = 1;
343 		break;
344 	default:
345 		mutex_exit(&proclist_lock);
346 		mutex_exit(&t->p_mutex);
347 		pheld = 0;
348 		break;
349 	}
350 
351 
352 	/* Do single-step fixup if needed. */
353 	FIX_SSTEP(t);
354 
355 	/*
356 	 * XXX NJWLWP
357 	 *
358 	 * The entire ptrace interface needs work to be useful to a
359 	 * process with multiple LWPs. For the moment, we'll kluge
360 	 * this; memory access will be fine, but register access will
361 	 * be weird.
362 	 */
363 	mutex_enter(&t->p_smutex);
364 	lt = proc_representative_lwp(t, NULL, 1);
365 	lwp_addref(lt);
366 	mutex_exit(&t->p_smutex);
367 
368 	/* Now do the operation. */
369 	write = 0;
370 	*retval = 0;
371 	tmp = 0;
372 
373 	switch (req) {
374 	case  PT_TRACE_ME:
375 		/* Just set the trace flag. */
376 		mutex_enter(&t->p_smutex);
377 		SET(t->p_slflag, PSL_TRACED);
378 		mutex_exit(&t->p_smutex);
379 		t->p_opptr = t->p_pptr;
380 		break;
381 
382 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
383 	case  PT_WRITE_D:
384 #if defined(__HAVE_RAS)
385 		/*
386 		 * Can't write to a RAS
387 		 */
388 		if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
389 			error = EACCES;
390 			break;
391 		}
392 #endif
393 		write = 1;
394 		tmp = SCARG(uap, data);
395 		/* FALLTHROUGH */
396 
397 	case  PT_READ_I:		/* XXX no separate I and D spaces */
398 	case  PT_READ_D:
399 		/* write = 0 done above. */
400 		iov.iov_base = (void *)&tmp;
401 		iov.iov_len = sizeof(tmp);
402 		uio.uio_iov = &iov;
403 		uio.uio_iovcnt = 1;
404 		uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr);
405 		uio.uio_resid = sizeof(tmp);
406 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
407 		UIO_SETUP_SYSSPACE(&uio);
408 
409 		error = process_domem(l, lt, &uio);
410 		if (!write)
411 			*retval = tmp;
412 		break;
413 
414 	case  PT_IO:
415 		error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
416 		if (error)
417 			break;
418 		switch (piod.piod_op) {
419 		case PIOD_READ_D:
420 		case PIOD_READ_I:
421 			uio.uio_rw = UIO_READ;
422 			break;
423 		case PIOD_WRITE_D:
424 		case PIOD_WRITE_I:
425 			/*
426 			 * Can't write to a RAS
427 			 */
428 			if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
429 				return (EACCES);
430 			}
431 			uio.uio_rw = UIO_WRITE;
432 			break;
433 		default:
434 			error = EINVAL;
435 			break;
436 		}
437 		if (error)
438 			break;
439 		error = proc_vmspace_getref(l->l_proc, &vm);
440 		if (error)
441 			break;
442 		iov.iov_base = piod.piod_addr;
443 		iov.iov_len = piod.piod_len;
444 		uio.uio_iov = &iov;
445 		uio.uio_iovcnt = 1;
446 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
447 		uio.uio_resid = piod.piod_len;
448 		uio.uio_vmspace = vm;
449 
450 		error = process_domem(l, lt, &uio);
451 		piod.piod_len -= uio.uio_resid;
452 		(void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
453 		uvmspace_free(vm);
454 		break;
455 
456 #ifdef COREDUMP
457 	case  PT_DUMPCORE:
458 		if ((path = SCARG(uap, addr)) != NULL) {
459 			char *dst;
460 			int len = SCARG(uap, data);
461 			if (len < 0 || len >= MAXPATHLEN) {
462 				error = EINVAL;
463 				break;
464 			}
465 			dst = malloc(len + 1, M_TEMP, M_WAITOK);
466 			if ((error = copyin(path, dst, len)) != 0) {
467 				free(dst, M_TEMP);
468 				break;
469 			}
470 			path = dst;
471 			path[len] = '\0';
472 		}
473 		error = coredump(lt, path);
474 		if (path)
475 			free(path, M_TEMP);
476 		break;
477 #endif
478 
479 #ifdef PT_STEP
480 	case  PT_STEP:
481 		/*
482 		 * From the 4.4BSD PRM:
483 		 * "Execution continues as in request PT_CONTINUE; however
484 		 * as soon as possible after execution of at least one
485 		 * instruction, execution stops again. [ ... ]"
486 		 */
487 #endif
488 	case  PT_CONTINUE:
489 	case  PT_SYSCALL:
490 	case  PT_DETACH:
491 		mutex_enter(&t->p_smutex);
492 		if (req == PT_SYSCALL) {
493 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
494 				SET(t->p_slflag, PSL_SYSCALL);
495 #ifdef __HAVE_SYSCALL_INTERN
496 				(*t->p_emul->e_syscall_intern)(t);
497 #endif
498 			}
499 		} else {
500 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
501 				CLR(t->p_slflag, PSL_SYSCALL);
502 #ifdef __HAVE_SYSCALL_INTERN
503 				(*t->p_emul->e_syscall_intern)(t);
504 #endif
505 			}
506 		}
507 		p->p_trace_enabled = trace_is_enabled(p);
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 		uvm_lwp_hold(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 				uvm_lwp_rele(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 			uvm_lwp_rele(lt);
544 			break;
545 		}
546 #endif
547 
548 		uvm_lwp_rele(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 		signo = SCARG(uap, data);
566 	sendsig:
567 		/* Finally, deliver the requested signal (or none). */
568 		mutex_enter(&proclist_mutex);
569 		mutex_enter(&t->p_smutex);
570 		if (t->p_stat == SSTOP) {
571 			/*
572 			 * Unstop the process.  If it needs to take a
573 			 * signal, make all efforts to ensure that at
574 			 * an LWP runs to see it.
575 			 */
576 			t->p_xstat = signo;
577 			proc_unstop(t);
578 		} else if (signo != 0) {
579 			KSI_INIT_EMPTY(&ksi);
580 			ksi.ksi_signo = signo;
581 			kpsignal2(t, &ksi);
582 		}
583 		mutex_exit(&t->p_smutex);
584 		mutex_exit(&proclist_mutex);
585 		break;
586 
587 	case  PT_KILL:
588 		/* just send the process a KILL signal. */
589 		signo = SIGKILL;
590 		goto sendsig;	/* in PT_CONTINUE, above. */
591 
592 	case  PT_ATTACH:
593 		/*
594 		 * Go ahead and set the trace flag.
595 		 * Save the old parent (it's reset in
596 		 *   _DETACH, and also in kern_exit.c:wait4()
597 		 * Reparent the process so that the tracing
598 		 *   proc gets to see all the action.
599 		 * Stop the target.
600 		 */
601 		t->p_opptr = t->p_pptr;
602 		if (t->p_pptr != p) {
603 			mutex_enter(&t->p_pptr->p_smutex);
604 			t->p_pptr->p_slflag |= PSL_CHTRACED;
605 			mutex_exit(&t->p_pptr->p_smutex);
606 			proc_reparent(t, p);
607 		}
608 		mutex_enter(&t->p_smutex);
609 		SET(t->p_slflag, PSL_TRACED);
610 		mutex_exit(&t->p_smutex);
611 		signo = SIGSTOP;
612 		goto sendsig;
613 
614 	case PT_LWPINFO:
615 		if (SCARG(uap, data) != sizeof(pl)) {
616 			error = EINVAL;
617 			break;
618 		}
619 		error = copyin(SCARG(uap, addr), &pl, sizeof(pl));
620 		if (error)
621 			break;
622 		tmp = pl.pl_lwpid;
623 		lwp_delref(lt);
624 		mutex_enter(&t->p_smutex);
625 		if (tmp == 0)
626 			lt = LIST_FIRST(&t->p_lwps);
627 		else {
628 			lt = lwp_find(p, tmp);
629 			if (lt == NULL) {
630 				mutex_exit(&t->p_smutex);
631 				error = ESRCH;
632 				break;
633 			}
634 			lt = LIST_NEXT(lt, l_sibling);
635 		}
636 		while (lt != NULL && lt->l_stat == LSZOMB)
637 			lt = LIST_NEXT(lt, l_sibling);
638 		pl.pl_lwpid = 0;
639 		pl.pl_event = 0;
640 		if (lt) {
641 			lwp_addref(lt);
642 			pl.pl_lwpid = lt->l_lid;
643 			if (lt->l_lid == t->p_sigctx.ps_lwp)
644 				pl.pl_event = PL_EVENT_SIGNAL;
645 		}
646 		mutex_exit(&t->p_smutex);
647 
648 		error = copyout(&pl, SCARG(uap, addr), sizeof(pl));
649 		break;
650 
651 #ifdef PT_SETREGS
652 	case  PT_SETREGS:
653 		write = 1;
654 #endif
655 #ifdef PT_GETREGS
656 	case  PT_GETREGS:
657 		/* write = 0 done above. */
658 #endif
659 #if defined(PT_SETREGS) || defined(PT_GETREGS)
660 		tmp = SCARG(uap, data);
661 		if (tmp != 0 && t->p_nlwps > 1) {
662 			lwp_delref(lt);
663 			mutex_enter(&t->p_smutex);
664 			lt = lwp_find(t, tmp);
665 			if (lt == NULL) {
666 				mutex_exit(&t->p_smutex);
667 				error = ESRCH;
668 				break;
669 			}
670 			lwp_addref(lt);
671 			mutex_exit(&t->p_smutex);
672 		}
673 		if (!process_validregs(lt))
674 			error = EINVAL;
675 		else {
676 			error = proc_vmspace_getref(l->l_proc, &vm);
677 			if (error)
678 				break;
679 			iov.iov_base = SCARG(uap, addr);
680 			iov.iov_len = sizeof(struct reg);
681 			uio.uio_iov = &iov;
682 			uio.uio_iovcnt = 1;
683 			uio.uio_offset = 0;
684 			uio.uio_resid = sizeof(struct reg);
685 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
686 			uio.uio_vmspace = vm;
687 
688 			error = process_doregs(l, lt, &uio);
689 			uvmspace_free(vm);
690 		}
691 		break;
692 #endif
693 
694 #ifdef PT_SETFPREGS
695 	case  PT_SETFPREGS:
696 		write = 1;
697 #endif
698 #ifdef PT_GETFPREGS
699 	case  PT_GETFPREGS:
700 		/* write = 0 done above. */
701 #endif
702 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
703 		tmp = SCARG(uap, data);
704 		if (tmp != 0 && t->p_nlwps > 1) {
705 			lwp_delref(lt);
706 			mutex_enter(&t->p_smutex);
707 			lt = lwp_find(t, tmp);
708 			if (lt == NULL) {
709 				mutex_exit(&t->p_smutex);
710 				error = ESRCH;
711 				break;
712 			}
713 			lwp_addref(lt);
714 			mutex_exit(&t->p_smutex);
715 		}
716 		if (!process_validfpregs(lt))
717 			error = EINVAL;
718 		else {
719 			error = proc_vmspace_getref(l->l_proc, &vm);
720 			if (error)
721 				break;
722 			iov.iov_base = SCARG(uap, addr);
723 			iov.iov_len = sizeof(struct fpreg);
724 			uio.uio_iov = &iov;
725 			uio.uio_iovcnt = 1;
726 			uio.uio_offset = 0;
727 			uio.uio_resid = sizeof(struct fpreg);
728 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
729 			uio.uio_vmspace = vm;
730 
731 			error = process_dofpregs(l, lt, &uio);
732 			uvmspace_free(vm);
733 		}
734 		break;
735 #endif
736 
737 #ifdef __HAVE_PTRACE_MACHDEP
738 	PTRACE_MACHDEP_REQUEST_CASES
739 		error = ptrace_machdep_dorequest(l, lt,
740 		    req, SCARG(uap, addr), SCARG(uap, data));
741 		break;
742 #endif
743 	}
744 
745 	if (lt != NULL)
746 		lwp_delref(lt);
747 	if (pheld) {
748 		mutex_exit(&t->p_mutex);
749 		mutex_exit(&proclist_lock);
750 	}
751 	rw_exit(&t->p_reflock);
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 	uvm_lwp_hold(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 	uvm_lwp_rele(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 	uvm_lwp_hold(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 	uvm_lwp_rele(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)
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 	mutex_enter(&vm->vm_map.misc_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 	mutex_exit(&vm->vm_map.misc_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 */
910 
911 #if defined(KTRACE) || defined(PTRACE)
912 void
913 process_stoptrace(void)
914 {
915 	struct lwp *l = curlwp;
916 	struct proc *p = l->l_proc, *pp;
917 
918 	/* XXXSMP proc_stop -> child_psignal -> kpsignal2 -> pool_get */
919 	KERNEL_LOCK(1, l);
920 
921 	mutex_enter(&proclist_mutex);
922 	mutex_enter(&p->p_smutex);
923 	pp = p->p_pptr;
924 	if (pp->p_pid == 1) {
925 		CLR(p->p_slflag, PSL_SYSCALL);	/* XXXSMP */
926 		mutex_exit(&p->p_smutex);
927 		mutex_exit(&proclist_mutex);
928 		KERNEL_UNLOCK_ONE(l);
929 		return;
930 	}
931 
932 	p->p_xstat = SIGTRAP;
933 	proc_stop(p, 1, SIGSTOP);
934 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
935 	mutex_exit(&proclist_mutex);
936 
937 	/*
938 	 * Call issignal() once only, to have it take care of the
939 	 * pending stop.  Signal processing will take place as usual
940 	 * from userret().
941 	 */
942 	(void)issignal(l);
943 	mutex_exit(&p->p_smutex);
944 	KERNEL_LOCK(l->l_biglocks - 1, l);
945 }
946 #endif	/* KTRACE || PTRACE */
947