xref: /netbsd-src/sys/kern/sys_process.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: sys_process.c,v 1.135 2008/01/23 15:04:40 elad 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.135 2008/01/23 15:04:40 elad 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 		mutex_exit(&t->p_smutex);
508 
509 		/*
510 		 * From the 4.4BSD PRM:
511 		 * "The data argument is taken as a signal number and the
512 		 * child's execution continues at location addr as if it
513 		 * incurred that signal.  Normally the signal number will
514 		 * be either 0 to indicate that the signal that caused the
515 		 * stop should be ignored, or that value fetched out of
516 		 * the process's image indicating which signal caused
517 		 * the stop.  If addr is (int *)1 then execution continues
518 		 * from where it stopped."
519 		 */
520 
521 		/* Check that the data is a valid signal number or zero. */
522 		if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) {
523 			error = EINVAL;
524 			break;
525 		}
526 
527 		uvm_lwp_hold(lt);
528 
529 		/* If the address parameter is not (int *)1, set the pc. */
530 		if ((int *)SCARG(uap, addr) != (int *)1)
531 			if ((error = process_set_pc(lt, SCARG(uap, addr))) != 0) {
532 				uvm_lwp_rele(lt);
533 				break;
534 			}
535 
536 #ifdef PT_STEP
537 		/*
538 		 * Arrange for a single-step, if that's requested and possible.
539 		 */
540 		error = process_sstep(lt, req == PT_STEP);
541 		if (error) {
542 			uvm_lwp_rele(lt);
543 			break;
544 		}
545 #endif
546 
547 		uvm_lwp_rele(lt);
548 
549 		if (req == PT_DETACH) {
550 			mutex_enter(&t->p_smutex);
551 			CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
552 			mutex_exit(&t->p_smutex);
553 
554 			/* give process back to original parent or init */
555 			if (t->p_opptr != t->p_pptr) {
556 				struct proc *pp = t->p_opptr;
557 				proc_reparent(t, pp ? pp : initproc);
558 			}
559 
560 			/* not being traced any more */
561 			t->p_opptr = NULL;
562 		}
563 
564 		signo = SCARG(uap, data);
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 = signo;
576 			proc_unstop(t);
577 		} else if (signo != 0) {
578 			KSI_INIT_EMPTY(&ksi);
579 			ksi.ksi_signo = signo;
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 		signo = 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 		signo = 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 		while (lt != NULL && lt->l_stat == LSZOMB)
636 			lt = LIST_NEXT(lt, l_sibling);
637 		pl.pl_lwpid = 0;
638 		pl.pl_event = 0;
639 		if (lt) {
640 			lwp_addref(lt);
641 			pl.pl_lwpid = lt->l_lid;
642 			if (lt->l_lid == t->p_sigctx.ps_lwp)
643 				pl.pl_event = PL_EVENT_SIGNAL;
644 		}
645 		mutex_exit(&t->p_smutex);
646 
647 		error = copyout(&pl, SCARG(uap, addr), sizeof(pl));
648 		break;
649 
650 #ifdef PT_SETREGS
651 	case  PT_SETREGS:
652 		write = 1;
653 #endif
654 #ifdef PT_GETREGS
655 	case  PT_GETREGS:
656 		/* write = 0 done above. */
657 #endif
658 #if defined(PT_SETREGS) || defined(PT_GETREGS)
659 		tmp = SCARG(uap, data);
660 		if (tmp != 0 && t->p_nlwps > 1) {
661 			lwp_delref(lt);
662 			mutex_enter(&t->p_smutex);
663 			lt = lwp_find(t, tmp);
664 			if (lt == NULL) {
665 				mutex_exit(&t->p_smutex);
666 				error = ESRCH;
667 				break;
668 			}
669 			lwp_addref(lt);
670 			mutex_exit(&t->p_smutex);
671 		}
672 		if (!process_validregs(lt))
673 			error = EINVAL;
674 		else {
675 			error = proc_vmspace_getref(l->l_proc, &vm);
676 			if (error)
677 				break;
678 			iov.iov_base = SCARG(uap, addr);
679 			iov.iov_len = sizeof(struct reg);
680 			uio.uio_iov = &iov;
681 			uio.uio_iovcnt = 1;
682 			uio.uio_offset = 0;
683 			uio.uio_resid = sizeof(struct reg);
684 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
685 			uio.uio_vmspace = vm;
686 
687 			error = process_doregs(l, lt, &uio);
688 			uvmspace_free(vm);
689 		}
690 		break;
691 #endif
692 
693 #ifdef PT_SETFPREGS
694 	case  PT_SETFPREGS:
695 		write = 1;
696 #endif
697 #ifdef PT_GETFPREGS
698 	case  PT_GETFPREGS:
699 		/* write = 0 done above. */
700 #endif
701 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
702 		tmp = SCARG(uap, data);
703 		if (tmp != 0 && t->p_nlwps > 1) {
704 			lwp_delref(lt);
705 			mutex_enter(&t->p_smutex);
706 			lt = lwp_find(t, tmp);
707 			if (lt == NULL) {
708 				mutex_exit(&t->p_smutex);
709 				error = ESRCH;
710 				break;
711 			}
712 			lwp_addref(lt);
713 			mutex_exit(&t->p_smutex);
714 		}
715 		if (!process_validfpregs(lt))
716 			error = EINVAL;
717 		else {
718 			error = proc_vmspace_getref(l->l_proc, &vm);
719 			if (error)
720 				break;
721 			iov.iov_base = SCARG(uap, addr);
722 			iov.iov_len = sizeof(struct fpreg);
723 			uio.uio_iov = &iov;
724 			uio.uio_iovcnt = 1;
725 			uio.uio_offset = 0;
726 			uio.uio_resid = sizeof(struct fpreg);
727 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
728 			uio.uio_vmspace = vm;
729 
730 			error = process_dofpregs(l, lt, &uio);
731 			uvmspace_free(vm);
732 		}
733 		break;
734 #endif
735 
736 #ifdef __HAVE_PTRACE_MACHDEP
737 	PTRACE_MACHDEP_REQUEST_CASES
738 		error = ptrace_machdep_dorequest(l, lt,
739 		    req, SCARG(uap, addr), SCARG(uap, data));
740 		break;
741 #endif
742 	}
743 
744 	if (lt != NULL)
745 		lwp_delref(lt);
746 	if (pheld) {
747 		mutex_exit(&t->p_mutex);
748 		mutex_exit(&proclist_lock);
749 	}
750 	rw_exit(&t->p_reflock);
751 
752 	return error;
753 }
754 
755 int
756 process_doregs(struct lwp *curl /*tracer*/,
757     struct lwp *l /*traced*/,
758     struct uio *uio)
759 {
760 #if defined(PT_GETREGS) || defined(PT_SETREGS)
761 	int error;
762 	struct reg r;
763 	char *kv;
764 	int kl;
765 
766 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
767 		return EINVAL;
768 
769 	kl = sizeof(r);
770 	kv = (char *)&r;
771 
772 	kv += uio->uio_offset;
773 	kl -= uio->uio_offset;
774 	if ((size_t)kl > uio->uio_resid)
775 		kl = uio->uio_resid;
776 
777 	uvm_lwp_hold(l);
778 
779 	error = process_read_regs(l, &r);
780 	if (error == 0)
781 		error = uiomove(kv, kl, uio);
782 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
783 		if (l->l_stat != LSSTOP)
784 			error = EBUSY;
785 		else
786 			error = process_write_regs(l, &r);
787 	}
788 
789 	uvm_lwp_rele(l);
790 
791 	uio->uio_offset = 0;
792 	return (error);
793 #else
794 	return (EINVAL);
795 #endif
796 }
797 
798 int
799 process_validregs(struct lwp *l)
800 {
801 
802 #if defined(PT_SETREGS) || defined(PT_GETREGS)
803 	return ((l->l_flag & LW_SYSTEM) == 0);
804 #else
805 	return (0);
806 #endif
807 }
808 
809 int
810 process_dofpregs(struct lwp *curl /*tracer*/,
811     struct lwp *l /*traced*/,
812     struct uio *uio)
813 {
814 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
815 	int error;
816 	struct fpreg r;
817 	char *kv;
818 	int kl;
819 
820 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
821 		return EINVAL;
822 
823 	kl = sizeof(r);
824 	kv = (char *)&r;
825 
826 	kv += uio->uio_offset;
827 	kl -= uio->uio_offset;
828 	if ((size_t)kl > uio->uio_resid)
829 		kl = uio->uio_resid;
830 
831 	uvm_lwp_hold(l);
832 
833 	error = process_read_fpregs(l, &r);
834 	if (error == 0)
835 		error = uiomove(kv, kl, uio);
836 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
837 		if (l->l_stat != LSSTOP)
838 			error = EBUSY;
839 		else
840 			error = process_write_fpregs(l, &r);
841 	}
842 
843 	uvm_lwp_rele(l);
844 
845 	uio->uio_offset = 0;
846 	return (error);
847 #else
848 	return (EINVAL);
849 #endif
850 }
851 
852 int
853 process_validfpregs(struct lwp *l)
854 {
855 
856 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
857 	return ((l->l_flag & LW_SYSTEM) == 0);
858 #else
859 	return (0);
860 #endif
861 }
862 #endif /* PTRACE */
863 
864 #if defined(KTRACE) || defined(PTRACE)
865 int
866 process_domem(struct lwp *curl /*tracer*/,
867     struct lwp *l /*traced*/,
868     struct uio *uio)
869 {
870 	struct proc *p = l->l_proc;	/* traced */
871 	struct vmspace *vm;
872 	int error;
873 
874 	size_t len;
875 #ifdef PMAP_NEED_PROCWR
876 	vaddr_t	addr;
877 #endif
878 
879 	error = 0;
880 	len = uio->uio_resid;
881 
882 	if (len == 0)
883 		return (0);
884 
885 #ifdef PMAP_NEED_PROCWR
886 	addr = uio->uio_offset;
887 #endif
888 
889 	vm = p->p_vmspace;
890 
891 	mutex_enter(&vm->vm_map.misc_lock);
892 	if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1)
893 		error = EFAULT;
894 	if (error == 0)
895 		p->p_vmspace->vm_refcnt++;  /* XXX */
896 	mutex_exit(&vm->vm_map.misc_lock);
897 	if (error != 0)
898 		return (error);
899 	error = uvm_io(&vm->vm_map, uio);
900 	uvmspace_free(vm);
901 
902 #ifdef PMAP_NEED_PROCWR
903 	if (error == 0 && uio->uio_rw == UIO_WRITE)
904 		pmap_procwr(p, addr, len);
905 #endif
906 	return (error);
907 }
908 #endif /* KTRACE || PTRACE */
909 
910 #if defined(KTRACE) || defined(PTRACE)
911 void
912 process_stoptrace(void)
913 {
914 	struct lwp *l = curlwp;
915 	struct proc *p = l->l_proc, *pp;
916 
917 	/* XXXSMP proc_stop -> child_psignal -> kpsignal2 -> pool_get */
918 	KERNEL_LOCK(1, l);
919 
920 	mutex_enter(&proclist_mutex);
921 	mutex_enter(&p->p_smutex);
922 	pp = p->p_pptr;
923 	if (pp->p_pid == 1) {
924 		CLR(p->p_slflag, PSL_SYSCALL);	/* XXXSMP */
925 		mutex_exit(&p->p_smutex);
926 		mutex_exit(&proclist_mutex);
927 		KERNEL_UNLOCK_ONE(l);
928 		return;
929 	}
930 
931 	p->p_xstat = SIGTRAP;
932 	proc_stop(p, 1, SIGSTOP);
933 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
934 	mutex_exit(&proclist_mutex);
935 
936 	/*
937 	 * Call issignal() once only, to have it take care of the
938 	 * pending stop.  Signal processing will take place as usual
939 	 * from userret().
940 	 */
941 	(void)issignal(l);
942 	mutex_exit(&p->p_smutex);
943 	KERNEL_LOCK(l->l_biglocks - 1, l);
944 }
945 #endif	/* KTRACE || PTRACE */
946