xref: /netbsd-src/sys/kern/sys_process.c (revision 0920b4f20b78ab1ccd9f2312fbe10deaf000cbf3)
1 /*	$NetBSD: sys_process.c,v 1.127 2007/07/21 19:21:53 ad 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.127 2007/07/21 19:21:53 ad 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(void *) 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 	mutex_enter(&proclist_lock);
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 			mutex_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 			mutex_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 		mutex_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 		mutex_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 		mutex_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)) != (void *)-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 = (void *)&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)) != (void *)-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 		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 	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 			lt = LIST_NEXT(lt, l_sibling);
634 		}
635 		pl.pl_lwpid = 0;
636 		pl.pl_event = 0;
637 		if (lt) {
638 			lwp_addref(lt);
639 			pl.pl_lwpid = lt->l_lid;
640 			if (lt->l_lid == t->p_sigctx.ps_lwp)
641 				pl.pl_event = PL_EVENT_SIGNAL;
642 		}
643 		mutex_exit(&t->p_smutex);
644 
645 		error = copyout(&pl, SCARG(uap, addr), sizeof(pl));
646 		break;
647 
648 #ifdef PT_SETREGS
649 	case  PT_SETREGS:
650 		write = 1;
651 #endif
652 #ifdef PT_GETREGS
653 	case  PT_GETREGS:
654 		/* write = 0 done above. */
655 #endif
656 #if defined(PT_SETREGS) || defined(PT_GETREGS)
657 		tmp = SCARG(uap, data);
658 		if (tmp != 0 && t->p_nlwps > 1) {
659 			lwp_delref(lt);
660 			mutex_enter(&t->p_smutex);
661 			lt = lwp_find(t, tmp);
662 			if (lt == NULL) {
663 				mutex_exit(&t->p_smutex);
664 				error = ESRCH;
665 				break;
666 			}
667 			lwp_addref(lt);
668 			mutex_exit(&t->p_smutex);
669 		}
670 		if (!process_validregs(lt))
671 			error = EINVAL;
672 		else {
673 			error = proc_vmspace_getref(l->l_proc, &vm);
674 			if (error)
675 				break;
676 			iov.iov_base = SCARG(uap, addr);
677 			iov.iov_len = sizeof(struct reg);
678 			uio.uio_iov = &iov;
679 			uio.uio_iovcnt = 1;
680 			uio.uio_offset = 0;
681 			uio.uio_resid = sizeof(struct reg);
682 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
683 			uio.uio_vmspace = vm;
684 
685 			error = process_doregs(l, lt, &uio);
686 			uvmspace_free(vm);
687 		}
688 		break;
689 #endif
690 
691 #ifdef PT_SETFPREGS
692 	case  PT_SETFPREGS:
693 		write = 1;
694 #endif
695 #ifdef PT_GETFPREGS
696 	case  PT_GETFPREGS:
697 		/* write = 0 done above. */
698 #endif
699 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
700 		tmp = SCARG(uap, data);
701 		if (tmp != 0 && t->p_nlwps > 1) {
702 			lwp_delref(lt);
703 			mutex_enter(&t->p_smutex);
704 			lt = lwp_find(t, tmp);
705 			if (lt == NULL) {
706 				mutex_exit(&t->p_smutex);
707 				error = ESRCH;
708 				break;
709 			}
710 			lwp_addref(lt);
711 			mutex_exit(&t->p_smutex);
712 		}
713 		if (!process_validfpregs(lt))
714 			error = EINVAL;
715 		else {
716 			error = proc_vmspace_getref(l->l_proc, &vm);
717 			if (error)
718 				break;
719 			iov.iov_base = SCARG(uap, addr);
720 			iov.iov_len = sizeof(struct fpreg);
721 			uio.uio_iov = &iov;
722 			uio.uio_iovcnt = 1;
723 			uio.uio_offset = 0;
724 			uio.uio_resid = sizeof(struct fpreg);
725 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
726 			uio.uio_vmspace = vm;
727 
728 			error = process_dofpregs(l, lt, &uio);
729 			uvmspace_free(vm);
730 		}
731 		break;
732 #endif
733 
734 #ifdef __HAVE_PTRACE_MACHDEP
735 	PTRACE_MACHDEP_REQUEST_CASES
736 		error = ptrace_machdep_dorequest(l, lt,
737 		    req, SCARG(uap, addr), SCARG(uap, data));
738 		break;
739 #endif
740 	}
741 
742 	if (lt != NULL)
743 		lwp_delref(lt);
744 
745 	if (pheld) {
746 		proc_delref(t);
747 		mutex_exit(&t->p_mutex);
748 		mutex_exit(&proclist_lock);
749 	} else {
750 		mutex_enter(&t->p_mutex);
751 		proc_delref(t);
752 		mutex_exit(&t->p_mutex);
753 	}
754 
755 	return error;
756 }
757 
758 int
759 process_doregs(struct lwp *curl /*tracer*/,
760     struct lwp *l /*traced*/,
761     struct uio *uio)
762 {
763 #if defined(PT_GETREGS) || defined(PT_SETREGS)
764 	int error;
765 	struct reg r;
766 	char *kv;
767 	int kl;
768 
769 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
770 		return EINVAL;
771 
772 	kl = sizeof(r);
773 	kv = (char *)&r;
774 
775 	kv += uio->uio_offset;
776 	kl -= uio->uio_offset;
777 	if ((size_t)kl > uio->uio_resid)
778 		kl = uio->uio_resid;
779 
780 	uvm_lwp_hold(l);
781 
782 	error = process_read_regs(l, &r);
783 	if (error == 0)
784 		error = uiomove(kv, kl, uio);
785 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
786 		if (l->l_stat != LSSTOP)
787 			error = EBUSY;
788 		else
789 			error = process_write_regs(l, &r);
790 	}
791 
792 	uvm_lwp_rele(l);
793 
794 	uio->uio_offset = 0;
795 	return (error);
796 #else
797 	return (EINVAL);
798 #endif
799 }
800 
801 int
802 process_validregs(struct lwp *l)
803 {
804 
805 #if defined(PT_SETREGS) || defined(PT_GETREGS)
806 	return ((l->l_flag & LW_SYSTEM) == 0);
807 #else
808 	return (0);
809 #endif
810 }
811 
812 int
813 process_dofpregs(struct lwp *curl /*tracer*/,
814     struct lwp *l /*traced*/,
815     struct uio *uio)
816 {
817 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
818 	int error;
819 	struct fpreg r;
820 	char *kv;
821 	int kl;
822 
823 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
824 		return EINVAL;
825 
826 	kl = sizeof(r);
827 	kv = (char *)&r;
828 
829 	kv += uio->uio_offset;
830 	kl -= uio->uio_offset;
831 	if ((size_t)kl > uio->uio_resid)
832 		kl = uio->uio_resid;
833 
834 	uvm_lwp_hold(l);
835 
836 	error = process_read_fpregs(l, &r);
837 	if (error == 0)
838 		error = uiomove(kv, kl, uio);
839 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
840 		if (l->l_stat != LSSTOP)
841 			error = EBUSY;
842 		else
843 			error = process_write_fpregs(l, &r);
844 	}
845 
846 	uvm_lwp_rele(l);
847 
848 	uio->uio_offset = 0;
849 	return (error);
850 #else
851 	return (EINVAL);
852 #endif
853 }
854 
855 int
856 process_validfpregs(struct lwp *l)
857 {
858 
859 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
860 	return ((l->l_flag & LW_SYSTEM) == 0);
861 #else
862 	return (0);
863 #endif
864 }
865 #endif /* PTRACE */
866 
867 #if defined(KTRACE) || defined(PTRACE) || defined(SYSTRACE)
868 int
869 process_domem(struct lwp *curl /*tracer*/,
870     struct lwp *l /*traced*/,
871     struct uio *uio)
872 {
873 	struct proc *p = l->l_proc;	/* traced */
874 	struct vmspace *vm;
875 	int error;
876 
877 	size_t len;
878 #ifdef PMAP_NEED_PROCWR
879 	vaddr_t	addr;
880 #endif
881 
882 	error = 0;
883 	len = uio->uio_resid;
884 
885 	if (len == 0)
886 		return (0);
887 
888 #ifdef PMAP_NEED_PROCWR
889 	addr = uio->uio_offset;
890 #endif
891 
892 	vm = p->p_vmspace;
893 
894 	mutex_enter(&vm->vm_map.misc_lock);
895 	if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1)
896 		error = EFAULT;
897 	if (error == 0)
898 		p->p_vmspace->vm_refcnt++;  /* XXX */
899 	mutex_exit(&vm->vm_map.misc_lock);
900 	if (error != 0)
901 		return (error);
902 	error = uvm_io(&vm->vm_map, uio);
903 	uvmspace_free(vm);
904 
905 #ifdef PMAP_NEED_PROCWR
906 	if (error == 0 && uio->uio_rw == UIO_WRITE)
907 		pmap_procwr(p, addr, len);
908 #endif
909 	return (error);
910 }
911 #endif /* KTRACE || PTRACE || SYSTRACE */
912 
913 #if defined(KTRACE) || defined(PTRACE)
914 void
915 process_stoptrace(struct lwp *l)
916 {
917 	struct proc *p = l->l_proc, *pp;
918 
919 	/* XXXSMP proc_stop -> child_psignal -> kpsignal2 -> pool_get */
920 	KERNEL_LOCK(1, l);
921 
922 	mutex_enter(&proclist_mutex);
923 	mutex_enter(&p->p_smutex);
924 	pp = p->p_pptr;
925 	if (pp->p_pid == 1) {
926 		CLR(p->p_slflag, PSL_SYSCALL);	/* XXXSMP */
927 		mutex_exit(&p->p_smutex);
928 		mutex_exit(&proclist_mutex);
929 		KERNEL_UNLOCK_ONE(l);
930 		return;
931 	}
932 
933 	p->p_xstat = SIGTRAP;
934 	proc_stop(p, 1, SIGSTOP);
935 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
936 	mutex_exit(&proclist_mutex);
937 
938 	/*
939 	 * Call issignal() once only, to have it take care of the
940 	 * pending stop.  Signal processing will take place as usual
941 	 * from userret().
942 	 */
943 	(void)issignal(l);
944 	mutex_exit(&p->p_smutex);
945 	KERNEL_LOCK(l->l_biglocks - 1, l);
946 }
947 #endif	/* KTRACE || PTRACE */
948