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