Home | History | Annotate | Line # | Download | only in kern
sys_ptrace_common.c revision 1.15
      1  1.15     kamil /*	$NetBSD: sys_ptrace_common.c,v 1.15 2017/02/22 23:43:43 kamil Exp $	*/
      2   1.1  pgoyette 
      3   1.1  pgoyette /*-
      4   1.1  pgoyette  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5   1.1  pgoyette  * All rights reserved.
      6   1.1  pgoyette  *
      7   1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  pgoyette  * by Andrew Doran.
      9   1.1  pgoyette  *
     10   1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11   1.1  pgoyette  * modification, are permitted provided that the following conditions
     12   1.1  pgoyette  * are met:
     13   1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14   1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15   1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18   1.1  pgoyette  *
     19   1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  pgoyette  */
     31   1.1  pgoyette 
     32   1.1  pgoyette /*-
     33   1.1  pgoyette  * Copyright (c) 1982, 1986, 1989, 1993
     34   1.1  pgoyette  *	The Regents of the University of California.  All rights reserved.
     35   1.1  pgoyette  * (c) UNIX System Laboratories, Inc.
     36   1.1  pgoyette  * All or some portions of this file are derived from material licensed
     37   1.1  pgoyette  * to the University of California by American Telephone and Telegraph
     38   1.1  pgoyette  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39   1.1  pgoyette  * the permission of UNIX System Laboratories, Inc.
     40   1.1  pgoyette  *
     41   1.1  pgoyette  * This code is derived from software contributed to Berkeley by
     42   1.1  pgoyette  * Jan-Simon Pendry.
     43   1.1  pgoyette  *
     44   1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     45   1.1  pgoyette  * modification, are permitted provided that the following conditions
     46   1.1  pgoyette  * are met:
     47   1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     48   1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     49   1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     51   1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     52   1.1  pgoyette  * 3. Neither the name of the University nor the names of its contributors
     53   1.1  pgoyette  *    may be used to endorse or promote products derived from this software
     54   1.1  pgoyette  *    without specific prior written permission.
     55   1.1  pgoyette  *
     56   1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57   1.1  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58   1.1  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59   1.1  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60   1.1  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61   1.1  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62   1.1  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63   1.1  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64   1.1  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65   1.1  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66   1.1  pgoyette  * SUCH DAMAGE.
     67   1.1  pgoyette  *
     68   1.1  pgoyette  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
     69   1.1  pgoyette  */
     70   1.1  pgoyette 
     71   1.1  pgoyette /*-
     72   1.1  pgoyette  * Copyright (c) 1993 Jan-Simon Pendry.
     73   1.1  pgoyette  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     74   1.1  pgoyette  *
     75   1.1  pgoyette  * This code is derived from software contributed to Berkeley by
     76   1.1  pgoyette  * Jan-Simon Pendry.
     77   1.1  pgoyette  *
     78   1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     79   1.1  pgoyette  * modification, are permitted provided that the following conditions
     80   1.1  pgoyette  * are met:
     81   1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     82   1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     83   1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     84   1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     85   1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     86   1.1  pgoyette  * 3. All advertising materials mentioning features or use of this software
     87   1.1  pgoyette  *    must display the following acknowledgement:
     88   1.1  pgoyette  *	This product includes software developed by the University of
     89   1.1  pgoyette  *	California, Berkeley and its contributors.
     90   1.1  pgoyette  * 4. Neither the name of the University nor the names of its contributors
     91   1.1  pgoyette  *    may be used to endorse or promote products derived from this software
     92   1.1  pgoyette  *    without specific prior written permission.
     93   1.1  pgoyette  *
     94   1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95   1.1  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96   1.1  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97   1.1  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98   1.1  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99   1.1  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100   1.1  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101   1.1  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102   1.1  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103   1.1  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104   1.1  pgoyette  * SUCH DAMAGE.
    105   1.1  pgoyette  *
    106   1.1  pgoyette  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
    107   1.1  pgoyette  */
    108   1.1  pgoyette 
    109   1.1  pgoyette /*
    110   1.1  pgoyette  * References:
    111   1.1  pgoyette  *	(1) Bach's "The Design of the UNIX Operating System",
    112   1.1  pgoyette  *	(2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
    113   1.1  pgoyette  *	(3) the "4.4BSD Programmer's Reference Manual" published
    114   1.1  pgoyette  *		by USENIX and O'Reilly & Associates.
    115   1.1  pgoyette  * The 4.4BSD PRM does a reasonably good job of documenting what the various
    116   1.1  pgoyette  * ptrace() requests should actually do, and its text is quoted several times
    117   1.1  pgoyette  * in this file.
    118   1.1  pgoyette  */
    119   1.1  pgoyette 
    120   1.1  pgoyette #include <sys/cdefs.h>
    121  1.15     kamil __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.15 2017/02/22 23:43:43 kamil Exp $");
    122   1.1  pgoyette 
    123   1.1  pgoyette #ifdef _KERNEL_OPT
    124   1.1  pgoyette #include "opt_ptrace.h"
    125   1.1  pgoyette #include "opt_ktrace.h"
    126   1.1  pgoyette #include "opt_pax.h"
    127   1.1  pgoyette #endif
    128   1.1  pgoyette 
    129   1.1  pgoyette #include <sys/param.h>
    130   1.1  pgoyette #include <sys/systm.h>
    131   1.1  pgoyette #include <sys/proc.h>
    132   1.1  pgoyette #include <sys/errno.h>
    133   1.1  pgoyette #include <sys/exec.h>
    134   1.1  pgoyette #include <sys/pax.h>
    135   1.1  pgoyette #include <sys/ptrace.h>
    136   1.1  pgoyette #include <sys/uio.h>
    137   1.1  pgoyette #include <sys/ras.h>
    138   1.1  pgoyette #include <sys/kmem.h>
    139   1.1  pgoyette #include <sys/kauth.h>
    140   1.1  pgoyette #include <sys/mount.h>
    141   1.1  pgoyette #include <sys/syscallargs.h>
    142   1.1  pgoyette #include <sys/module.h>
    143   1.1  pgoyette #include <sys/condvar.h>
    144   1.1  pgoyette #include <sys/mutex.h>
    145   1.1  pgoyette 
    146   1.1  pgoyette #include <uvm/uvm_extern.h>
    147   1.1  pgoyette 
    148   1.1  pgoyette #include <machine/reg.h>
    149   1.1  pgoyette 
    150   1.1  pgoyette #ifdef PTRACE
    151   1.1  pgoyette 
    152   1.1  pgoyette # ifdef DEBUG
    153   1.1  pgoyette #  define DPRINTF(a) uprintf a
    154   1.1  pgoyette # else
    155   1.1  pgoyette #  define DPRINTF(a)
    156   1.1  pgoyette # endif
    157   1.1  pgoyette 
    158   1.1  pgoyette static kauth_listener_t ptrace_listener;
    159   1.1  pgoyette static int process_auxv_offset(struct proc *, struct uio *);
    160   1.1  pgoyette 
    161   1.1  pgoyette #if 0
    162   1.1  pgoyette static int ptrace_cbref;
    163   1.1  pgoyette static kmutex_t ptrace_mtx;
    164   1.1  pgoyette static kcondvar_t ptrace_cv;
    165   1.1  pgoyette #endif
    166   1.1  pgoyette 
    167   1.1  pgoyette static int
    168   1.1  pgoyette ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    169   1.1  pgoyette     void *arg0, void *arg1, void *arg2, void *arg3)
    170   1.1  pgoyette {
    171   1.1  pgoyette 	struct proc *p;
    172   1.1  pgoyette 	int result;
    173   1.1  pgoyette 
    174   1.1  pgoyette 	result = KAUTH_RESULT_DEFER;
    175   1.1  pgoyette 	p = arg0;
    176   1.1  pgoyette 
    177   1.1  pgoyette #if 0
    178   1.1  pgoyette 	mutex_enter(&ptrace_mtx);
    179   1.1  pgoyette 	ptrace_cbref++;
    180   1.1  pgoyette 	mutex_exit(&ptrace_mtx);
    181   1.1  pgoyette #endif
    182   1.1  pgoyette 	if (action != KAUTH_PROCESS_PTRACE)
    183   1.1  pgoyette 		goto out;
    184   1.1  pgoyette 
    185   1.1  pgoyette 	switch ((u_long)arg1) {
    186   1.1  pgoyette 	case PT_TRACE_ME:
    187   1.1  pgoyette 	case PT_ATTACH:
    188   1.1  pgoyette 	case PT_WRITE_I:
    189   1.1  pgoyette 	case PT_WRITE_D:
    190   1.1  pgoyette 	case PT_READ_I:
    191   1.1  pgoyette 	case PT_READ_D:
    192   1.1  pgoyette 	case PT_IO:
    193   1.1  pgoyette #ifdef PT_GETREGS
    194   1.1  pgoyette 	case PT_GETREGS:
    195   1.1  pgoyette #endif
    196   1.1  pgoyette #ifdef PT_SETREGS
    197   1.1  pgoyette 	case PT_SETREGS:
    198   1.1  pgoyette #endif
    199   1.1  pgoyette #ifdef PT_GETFPREGS
    200   1.1  pgoyette 	case PT_GETFPREGS:
    201   1.1  pgoyette #endif
    202   1.1  pgoyette #ifdef PT_SETFPREGS
    203   1.1  pgoyette 	case PT_SETFPREGS:
    204   1.1  pgoyette #endif
    205   1.7     kamil #ifdef __HAVE_PTRACE_WATCHPOINTS
    206   1.7     kamil 	case PT_READ_WATCHPOINT:
    207   1.7     kamil 	case PT_WRITE_WATCHPOINT:
    208   1.7     kamil 	case PT_COUNT_WATCHPOINTS:
    209   1.7     kamil #endif
    210   1.1  pgoyette 	case PT_SET_EVENT_MASK:
    211   1.1  pgoyette 	case PT_GET_EVENT_MASK:
    212   1.1  pgoyette 	case PT_GET_PROCESS_STATE:
    213   1.8     kamil 	case PT_SET_SIGINFO:
    214   1.8     kamil 	case PT_GET_SIGINFO:
    215  1.14     kamil 	case PT_SET_SIGMASK:
    216  1.14     kamil 	case PT_GET_SIGMASK:
    217   1.1  pgoyette #ifdef __HAVE_PTRACE_MACHDEP
    218   1.1  pgoyette 	PTRACE_MACHDEP_REQUEST_CASES
    219   1.1  pgoyette #endif
    220   1.1  pgoyette 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    221   1.1  pgoyette 		    ISSET(p->p_flag, PK_SUGID)) {
    222   1.1  pgoyette 			break;
    223   1.1  pgoyette 		}
    224   1.1  pgoyette 
    225   1.1  pgoyette 		result = KAUTH_RESULT_ALLOW;
    226   1.1  pgoyette 
    227   1.1  pgoyette 	break;
    228   1.1  pgoyette 
    229   1.1  pgoyette #ifdef PT_STEP
    230   1.1  pgoyette 	case PT_STEP:
    231   1.1  pgoyette #endif
    232   1.1  pgoyette 	case PT_CONTINUE:
    233   1.1  pgoyette 	case PT_KILL:
    234   1.1  pgoyette 	case PT_DETACH:
    235   1.1  pgoyette 	case PT_LWPINFO:
    236   1.1  pgoyette 	case PT_SYSCALL:
    237   1.1  pgoyette 	case PT_SYSCALLEMU:
    238   1.1  pgoyette 	case PT_DUMPCORE:
    239  1.15     kamil 	case PT_RESUME:
    240  1.15     kamil 	case PT_SUSPEND:
    241   1.1  pgoyette 		result = KAUTH_RESULT_ALLOW;
    242   1.1  pgoyette 		break;
    243   1.1  pgoyette 
    244   1.1  pgoyette 	default:
    245   1.1  pgoyette 		break;
    246   1.1  pgoyette 	}
    247   1.1  pgoyette 
    248   1.1  pgoyette  out:
    249   1.1  pgoyette #if 0
    250   1.1  pgoyette 	mutex_enter(&ptrace_mtx);
    251   1.1  pgoyette 	if (--ptrace_cbref == 0)
    252   1.1  pgoyette 		cv_broadcast(&ptrace_cv);
    253   1.1  pgoyette 	mutex_exit(&ptrace_mtx);
    254   1.1  pgoyette #endif
    255   1.1  pgoyette 
    256   1.1  pgoyette 	return result;
    257   1.1  pgoyette }
    258   1.1  pgoyette 
    259   1.1  pgoyette int
    260   1.1  pgoyette ptrace_init(void)
    261   1.1  pgoyette {
    262   1.1  pgoyette 
    263   1.1  pgoyette #if 0
    264   1.1  pgoyette 	mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
    265   1.1  pgoyette 	cv_init(&ptrace_cv, "ptracecb");
    266   1.1  pgoyette 	ptrace_cbref = 0;
    267   1.1  pgoyette #endif
    268   1.1  pgoyette 	ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    269   1.1  pgoyette 	    ptrace_listener_cb, NULL);
    270   1.1  pgoyette 	return 0;
    271   1.1  pgoyette }
    272   1.1  pgoyette 
    273   1.1  pgoyette int
    274   1.1  pgoyette ptrace_fini(void)
    275   1.1  pgoyette {
    276   1.1  pgoyette 
    277   1.1  pgoyette 	kauth_unlisten_scope(ptrace_listener);
    278   1.1  pgoyette 
    279   1.1  pgoyette #if 0
    280   1.1  pgoyette 	/* Make sure no-one is executing our kauth listener */
    281   1.1  pgoyette 
    282   1.1  pgoyette 	mutex_enter(&ptrace_mtx);
    283   1.1  pgoyette 	while (ptrace_cbref != 0)
    284   1.1  pgoyette 		cv_wait(&ptrace_cv, &ptrace_mtx);
    285   1.1  pgoyette 	mutex_exit(&ptrace_mtx);
    286   1.1  pgoyette 	mutex_destroy(&ptrace_mtx);
    287   1.1  pgoyette 	cv_destroy(&ptrace_cv);
    288   1.1  pgoyette #endif
    289   1.1  pgoyette 
    290   1.1  pgoyette 	return 0;
    291   1.1  pgoyette }
    292   1.1  pgoyette 
    293   1.1  pgoyette int
    294   1.1  pgoyette do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
    295   1.1  pgoyette     void *addr, int data, register_t *retval)
    296   1.1  pgoyette {
    297   1.1  pgoyette 	struct proc *p = l->l_proc;
    298   1.1  pgoyette 	struct lwp *lt;
    299   1.1  pgoyette #ifdef PT_STEP
    300   1.1  pgoyette 	struct lwp *lt2;
    301   1.1  pgoyette #endif
    302   1.1  pgoyette 	struct proc *t;				/* target process */
    303   1.1  pgoyette 	struct uio uio;
    304   1.1  pgoyette 	struct iovec iov;
    305   1.1  pgoyette 	struct ptrace_io_desc piod;
    306   1.1  pgoyette 	struct ptrace_event pe;
    307   1.1  pgoyette 	struct ptrace_state ps;
    308   1.1  pgoyette 	struct ptrace_lwpinfo pl;
    309   1.8     kamil 	struct ptrace_siginfo psi;
    310   1.7     kamil #ifdef __HAVE_PTRACE_WATCHPOINTS
    311   1.7     kamil 	struct ptrace_watchpoint pw;
    312   1.7     kamil #endif
    313   1.1  pgoyette 	struct vmspace *vm;
    314   1.1  pgoyette 	int error, write, tmp, pheld;
    315   1.1  pgoyette 	int signo = 0;
    316   1.1  pgoyette 	int resume_all;
    317   1.1  pgoyette 	ksiginfo_t ksi;
    318   1.1  pgoyette 	char *path;
    319   1.1  pgoyette 	int len = 0;
    320   1.1  pgoyette 	error = 0;
    321   1.1  pgoyette 
    322   1.1  pgoyette 	/*
    323   1.1  pgoyette 	 * If attaching or detaching, we need to get a write hold on the
    324   1.1  pgoyette 	 * proclist lock so that we can re-parent the target process.
    325   1.1  pgoyette 	 */
    326   1.1  pgoyette 	mutex_enter(proc_lock);
    327   1.1  pgoyette 
    328   1.1  pgoyette 	/* "A foolish consistency..." XXX */
    329   1.1  pgoyette 	if (req == PT_TRACE_ME) {
    330   1.1  pgoyette 		t = p;
    331   1.1  pgoyette 		mutex_enter(t->p_lock);
    332   1.1  pgoyette 	} else {
    333   1.1  pgoyette 		/* Find the process we're supposed to be operating on. */
    334   1.1  pgoyette 		t = proc_find(pid);
    335   1.1  pgoyette 		if (t == NULL) {
    336   1.1  pgoyette 			mutex_exit(proc_lock);
    337   1.1  pgoyette 			return ESRCH;
    338   1.1  pgoyette 		}
    339   1.1  pgoyette 
    340   1.1  pgoyette 		/* XXX-elad */
    341   1.1  pgoyette 		mutex_enter(t->p_lock);
    342   1.1  pgoyette 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
    343   1.1  pgoyette 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    344   1.1  pgoyette 		if (error) {
    345   1.1  pgoyette 			mutex_exit(proc_lock);
    346   1.1  pgoyette 			mutex_exit(t->p_lock);
    347   1.1  pgoyette 			return ESRCH;
    348   1.1  pgoyette 		}
    349   1.1  pgoyette 	}
    350   1.1  pgoyette 
    351   1.1  pgoyette 	/*
    352   1.1  pgoyette 	 * Grab a reference on the process to prevent it from execing or
    353   1.1  pgoyette 	 * exiting.
    354   1.1  pgoyette 	 */
    355   1.1  pgoyette 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
    356   1.1  pgoyette 		mutex_exit(proc_lock);
    357   1.1  pgoyette 		mutex_exit(t->p_lock);
    358   1.1  pgoyette 		return EBUSY;
    359   1.1  pgoyette 	}
    360   1.1  pgoyette 
    361   1.1  pgoyette 	/* Make sure we can operate on it. */
    362   1.1  pgoyette 	switch (req) {
    363   1.1  pgoyette 	case  PT_TRACE_ME:
    364   1.1  pgoyette 		/* Saying that you're being traced is always legal. */
    365   1.1  pgoyette 		break;
    366   1.1  pgoyette 
    367   1.1  pgoyette 	case  PT_ATTACH:
    368   1.1  pgoyette 		/*
    369   1.1  pgoyette 		 * You can't attach to a process if:
    370   1.1  pgoyette 		 *	(1) it's the process that's doing the attaching,
    371   1.1  pgoyette 		 */
    372   1.1  pgoyette 		if (t->p_pid == p->p_pid) {
    373   1.1  pgoyette 			error = EINVAL;
    374   1.1  pgoyette 			break;
    375   1.1  pgoyette 		}
    376   1.1  pgoyette 
    377   1.1  pgoyette 		/*
    378   1.1  pgoyette 		 *  (2) it's a system process
    379   1.1  pgoyette 		 */
    380   1.1  pgoyette 		if (t->p_flag & PK_SYSTEM) {
    381   1.1  pgoyette 			error = EPERM;
    382   1.1  pgoyette 			break;
    383   1.1  pgoyette 		}
    384   1.1  pgoyette 
    385   1.1  pgoyette 		/*
    386   1.1  pgoyette 		 *	(3) it's already being traced, or
    387   1.1  pgoyette 		 */
    388   1.1  pgoyette 		if (ISSET(t->p_slflag, PSL_TRACED)) {
    389   1.1  pgoyette 			error = EBUSY;
    390   1.1  pgoyette 			break;
    391   1.1  pgoyette 		}
    392   1.1  pgoyette 
    393   1.1  pgoyette 		/*
    394   1.1  pgoyette 		 * 	(4) the tracer is chrooted, and its root directory is
    395   1.1  pgoyette 		 * 	    not at or above the root directory of the tracee
    396   1.1  pgoyette 		 */
    397   1.1  pgoyette 		mutex_exit(t->p_lock);	/* XXXSMP */
    398   1.1  pgoyette 		tmp = proc_isunder(t, l);
    399   1.1  pgoyette 		mutex_enter(t->p_lock);	/* XXXSMP */
    400   1.1  pgoyette 		if (!tmp) {
    401   1.1  pgoyette 			error = EPERM;
    402   1.1  pgoyette 			break;
    403   1.1  pgoyette 		}
    404   1.1  pgoyette 		break;
    405   1.1  pgoyette 
    406   1.1  pgoyette 	case  PT_READ_I:
    407   1.1  pgoyette 	case  PT_READ_D:
    408   1.1  pgoyette 	case  PT_WRITE_I:
    409   1.1  pgoyette 	case  PT_WRITE_D:
    410   1.1  pgoyette 	case  PT_IO:
    411  1.13     kamil 	case  PT_SET_SIGINFO:
    412  1.13     kamil 	case  PT_GET_SIGINFO:
    413  1.14     kamil 	case  PT_SET_SIGMASK:
    414  1.14     kamil 	case  PT_GET_SIGMASK:
    415   1.1  pgoyette #ifdef PT_GETREGS
    416   1.1  pgoyette 	case  PT_GETREGS:
    417   1.1  pgoyette #endif
    418   1.1  pgoyette #ifdef PT_SETREGS
    419   1.1  pgoyette 	case  PT_SETREGS:
    420   1.1  pgoyette #endif
    421   1.1  pgoyette #ifdef PT_GETFPREGS
    422   1.1  pgoyette 	case  PT_GETFPREGS:
    423   1.1  pgoyette #endif
    424   1.1  pgoyette #ifdef PT_SETFPREGS
    425   1.1  pgoyette 	case  PT_SETFPREGS:
    426   1.1  pgoyette #endif
    427   1.7     kamil #ifdef __HAVE_PTRACE_WATCHPOINTS
    428   1.7     kamil 	case  PT_READ_WATCHPOINT:
    429   1.7     kamil 	case  PT_WRITE_WATCHPOINT:
    430   1.7     kamil 	case  PT_COUNT_WATCHPOINTS:
    431   1.7     kamil #endif
    432   1.1  pgoyette #ifdef __HAVE_PTRACE_MACHDEP
    433   1.1  pgoyette 	PTRACE_MACHDEP_REQUEST_CASES
    434   1.1  pgoyette #endif
    435   1.1  pgoyette 		/*
    436   1.1  pgoyette 		 * You can't read/write the memory or registers of a process
    437   1.1  pgoyette 		 * if the tracer is chrooted, and its root directory is not at
    438   1.1  pgoyette 		 * or above the root directory of the tracee.
    439   1.1  pgoyette 		 */
    440   1.1  pgoyette 		mutex_exit(t->p_lock);	/* XXXSMP */
    441   1.1  pgoyette 		tmp = proc_isunder(t, l);
    442   1.1  pgoyette 		mutex_enter(t->p_lock);	/* XXXSMP */
    443   1.1  pgoyette 		if (!tmp) {
    444   1.1  pgoyette 			error = EPERM;
    445   1.1  pgoyette 			break;
    446   1.1  pgoyette 		}
    447   1.1  pgoyette 		/*FALLTHROUGH*/
    448   1.1  pgoyette 
    449   1.1  pgoyette 	case  PT_CONTINUE:
    450   1.1  pgoyette 	case  PT_KILL:
    451   1.1  pgoyette 	case  PT_DETACH:
    452   1.1  pgoyette 	case  PT_LWPINFO:
    453   1.1  pgoyette 	case  PT_SYSCALL:
    454   1.1  pgoyette 	case  PT_SYSCALLEMU:
    455   1.1  pgoyette 	case  PT_DUMPCORE:
    456   1.1  pgoyette #ifdef PT_STEP
    457   1.1  pgoyette 	case  PT_STEP:
    458   1.1  pgoyette #endif
    459   1.1  pgoyette 	case  PT_SET_EVENT_MASK:
    460   1.1  pgoyette 	case  PT_GET_EVENT_MASK:
    461   1.1  pgoyette 	case  PT_GET_PROCESS_STATE:
    462  1.15     kamil 	case  PT_RESUME:
    463  1.15     kamil 	case  PT_SUSPEND:
    464   1.1  pgoyette 		/*
    465   1.1  pgoyette 		 * You can't do what you want to the process if:
    466   1.1  pgoyette 		 *	(1) It's not being traced at all,
    467   1.1  pgoyette 		 */
    468   1.1  pgoyette 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
    469   1.1  pgoyette 			error = EPERM;
    470   1.1  pgoyette 			break;
    471   1.1  pgoyette 		}
    472   1.1  pgoyette 
    473   1.1  pgoyette 		/*
    474   1.1  pgoyette 		 *	(2) it's being traced by procfs (which has
    475   1.1  pgoyette 		 *	    different signal delivery semantics),
    476   1.1  pgoyette 		 */
    477   1.1  pgoyette 		if (ISSET(t->p_slflag, PSL_FSTRACE)) {
    478   1.1  pgoyette 			DPRINTF(("file system traced\n"));
    479   1.1  pgoyette 			error = EBUSY;
    480   1.1  pgoyette 			break;
    481   1.1  pgoyette 		}
    482   1.1  pgoyette 
    483   1.1  pgoyette 		/*
    484   1.1  pgoyette 		 *	(3) it's not being traced by _you_, or
    485   1.1  pgoyette 		 */
    486   1.1  pgoyette 		if (t->p_pptr != p) {
    487   1.1  pgoyette 			DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
    488   1.1  pgoyette 			    p->p_pid));
    489   1.1  pgoyette 			error = EBUSY;
    490   1.1  pgoyette 			break;
    491   1.1  pgoyette 		}
    492   1.1  pgoyette 
    493   1.1  pgoyette 		/*
    494   1.1  pgoyette 		 *	(4) it's not currently stopped.
    495   1.1  pgoyette 		 */
    496   1.1  pgoyette 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
    497   1.1  pgoyette 			DPRINTF(("stat %d flag %d\n", t->p_stat,
    498   1.1  pgoyette 			    !t->p_waited));
    499   1.1  pgoyette 			error = EBUSY;
    500   1.1  pgoyette 			break;
    501   1.1  pgoyette 		}
    502   1.1  pgoyette 		break;
    503   1.1  pgoyette 
    504   1.1  pgoyette 	default:			/* It was not a legal request. */
    505   1.1  pgoyette 		error = EINVAL;
    506   1.1  pgoyette 		break;
    507   1.1  pgoyette 	}
    508   1.1  pgoyette 
    509   1.1  pgoyette 	if (error == 0) {
    510   1.1  pgoyette 		error = kauth_authorize_process(l->l_cred,
    511   1.1  pgoyette 		    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
    512   1.1  pgoyette 		    NULL, NULL);
    513   1.1  pgoyette 	}
    514   1.1  pgoyette 	if (error == 0) {
    515   1.1  pgoyette 		lt = lwp_find_first(t);
    516   1.1  pgoyette 		if (lt == NULL)
    517   1.1  pgoyette 			error = ESRCH;
    518   1.1  pgoyette 	}
    519   1.1  pgoyette 
    520   1.1  pgoyette 	if (error != 0) {
    521   1.1  pgoyette 		mutex_exit(proc_lock);
    522   1.1  pgoyette 		mutex_exit(t->p_lock);
    523   1.1  pgoyette 		rw_exit(&t->p_reflock);
    524   1.1  pgoyette 		return error;
    525   1.1  pgoyette 	}
    526   1.1  pgoyette 
    527   1.1  pgoyette 	/* Do single-step fixup if needed. */
    528   1.1  pgoyette 	FIX_SSTEP(t);
    529   1.1  pgoyette 	KASSERT(lt != NULL);
    530   1.1  pgoyette 	lwp_addref(lt);
    531   1.1  pgoyette 
    532   1.1  pgoyette 	/*
    533   1.1  pgoyette 	 * Which locks do we need held? XXX Ugly.
    534   1.1  pgoyette 	 */
    535   1.1  pgoyette 	switch (req) {
    536   1.1  pgoyette #ifdef PT_STEP
    537   1.1  pgoyette 	case PT_STEP:
    538   1.1  pgoyette #endif
    539   1.1  pgoyette 	case PT_CONTINUE:
    540   1.1  pgoyette 	case PT_DETACH:
    541   1.1  pgoyette 	case PT_KILL:
    542   1.1  pgoyette 	case PT_SYSCALL:
    543   1.1  pgoyette 	case PT_SYSCALLEMU:
    544   1.1  pgoyette 	case PT_ATTACH:
    545   1.1  pgoyette 	case PT_TRACE_ME:
    546   1.1  pgoyette 		pheld = 1;
    547   1.1  pgoyette 		break;
    548   1.1  pgoyette 	default:
    549   1.1  pgoyette 		mutex_exit(proc_lock);
    550   1.1  pgoyette 		mutex_exit(t->p_lock);
    551   1.1  pgoyette 		pheld = 0;
    552   1.1  pgoyette 		break;
    553   1.1  pgoyette 	}
    554   1.1  pgoyette 
    555   1.1  pgoyette 	/* Now do the operation. */
    556   1.1  pgoyette 	write = 0;
    557   1.1  pgoyette 	*retval = 0;
    558   1.1  pgoyette 	tmp = 0;
    559   1.1  pgoyette 	resume_all = 1;
    560   1.1  pgoyette 
    561   1.1  pgoyette 	switch (req) {
    562   1.1  pgoyette 	case  PT_TRACE_ME:
    563   1.1  pgoyette 		/* Just set the trace flag. */
    564   1.1  pgoyette 		SET(t->p_slflag, PSL_TRACED);
    565   1.1  pgoyette 		t->p_opptr = t->p_pptr;
    566   1.1  pgoyette 		break;
    567   1.1  pgoyette 
    568   1.1  pgoyette 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
    569   1.1  pgoyette 	case  PT_WRITE_D:
    570   1.1  pgoyette #if defined(__HAVE_RAS)
    571   1.1  pgoyette 		/*
    572   1.1  pgoyette 		 * Can't write to a RAS
    573   1.1  pgoyette 		 */
    574   1.1  pgoyette 		if (ras_lookup(t, addr) != (void *)-1) {
    575   1.1  pgoyette 			error = EACCES;
    576   1.1  pgoyette 			break;
    577   1.1  pgoyette 		}
    578   1.1  pgoyette #endif
    579   1.1  pgoyette 		write = 1;
    580   1.1  pgoyette 		tmp = data;
    581   1.1  pgoyette 		/* FALLTHROUGH */
    582   1.1  pgoyette 
    583   1.1  pgoyette 	case  PT_READ_I:		/* XXX no separate I and D spaces */
    584   1.1  pgoyette 	case  PT_READ_D:
    585   1.1  pgoyette 		/* write = 0 done above. */
    586   1.1  pgoyette 		iov.iov_base = (void *)&tmp;
    587   1.1  pgoyette 		iov.iov_len = sizeof(tmp);
    588   1.1  pgoyette 		uio.uio_iov = &iov;
    589   1.1  pgoyette 		uio.uio_iovcnt = 1;
    590   1.1  pgoyette 		uio.uio_offset = (off_t)(unsigned long)addr;
    591   1.1  pgoyette 		uio.uio_resid = sizeof(tmp);
    592   1.1  pgoyette 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
    593   1.1  pgoyette 		UIO_SETUP_SYSSPACE(&uio);
    594   1.1  pgoyette 
    595   1.1  pgoyette 		error = process_domem(l, lt, &uio);
    596   1.1  pgoyette 		if (!write)
    597   1.1  pgoyette 			*retval = tmp;
    598   1.1  pgoyette 		break;
    599   1.1  pgoyette 
    600   1.1  pgoyette 	case  PT_IO:
    601   1.1  pgoyette 		error = ptm->ptm_copyinpiod(&piod, addr);
    602   1.1  pgoyette 		if (error)
    603   1.1  pgoyette 			break;
    604   1.1  pgoyette 
    605   1.1  pgoyette 		iov.iov_base = piod.piod_addr;
    606   1.1  pgoyette 		iov.iov_len = piod.piod_len;
    607   1.1  pgoyette 		uio.uio_iov = &iov;
    608   1.1  pgoyette 		uio.uio_iovcnt = 1;
    609   1.1  pgoyette 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
    610   1.1  pgoyette 		uio.uio_resid = piod.piod_len;
    611   1.1  pgoyette 
    612   1.1  pgoyette 		switch (piod.piod_op) {
    613   1.1  pgoyette 		case PIOD_READ_D:
    614   1.1  pgoyette 		case PIOD_READ_I:
    615   1.1  pgoyette 			uio.uio_rw = UIO_READ;
    616   1.1  pgoyette 			break;
    617   1.1  pgoyette 		case PIOD_WRITE_D:
    618   1.1  pgoyette 		case PIOD_WRITE_I:
    619   1.1  pgoyette 			/*
    620   1.1  pgoyette 			 * Can't write to a RAS
    621   1.1  pgoyette 			 */
    622   1.1  pgoyette 			if (ras_lookup(t, addr) != (void *)-1) {
    623   1.1  pgoyette 				return EACCES;
    624   1.1  pgoyette 			}
    625   1.1  pgoyette 			uio.uio_rw = UIO_WRITE;
    626   1.1  pgoyette 			break;
    627   1.1  pgoyette 		case PIOD_READ_AUXV:
    628   1.1  pgoyette 			req = PT_READ_D;
    629   1.1  pgoyette 			uio.uio_rw = UIO_READ;
    630  1.11  christos 			tmp = t->p_execsw->es_arglen;
    631   1.1  pgoyette 			if (uio.uio_offset > tmp)
    632   1.1  pgoyette 				return EIO;
    633   1.1  pgoyette 			if (uio.uio_resid > tmp - uio.uio_offset)
    634   1.1  pgoyette 				uio.uio_resid = tmp - uio.uio_offset;
    635   1.1  pgoyette 			piod.piod_len = iov.iov_len = uio.uio_resid;
    636   1.1  pgoyette 			error = process_auxv_offset(t, &uio);
    637   1.1  pgoyette 			break;
    638   1.1  pgoyette 		default:
    639   1.1  pgoyette 			error = EINVAL;
    640   1.1  pgoyette 			break;
    641   1.1  pgoyette 		}
    642   1.1  pgoyette 		if (error)
    643   1.1  pgoyette 			break;
    644   1.1  pgoyette 		error = proc_vmspace_getref(l->l_proc, &vm);
    645   1.1  pgoyette 		if (error)
    646   1.1  pgoyette 			break;
    647   1.1  pgoyette 		uio.uio_vmspace = vm;
    648   1.1  pgoyette 
    649   1.1  pgoyette 		error = process_domem(l, lt, &uio);
    650   1.1  pgoyette 		piod.piod_len -= uio.uio_resid;
    651   1.1  pgoyette 		(void) ptm->ptm_copyoutpiod(&piod, addr);
    652   1.1  pgoyette 
    653   1.1  pgoyette 		uvmspace_free(vm);
    654   1.1  pgoyette 		break;
    655   1.1  pgoyette 
    656   1.1  pgoyette 	case  PT_DUMPCORE:
    657   1.1  pgoyette 		if ((path = addr) != NULL) {
    658   1.1  pgoyette 			char *dst;
    659   1.1  pgoyette 			len = data;
    660   1.1  pgoyette 
    661   1.1  pgoyette 			if (len < 0 || len >= MAXPATHLEN) {
    662   1.1  pgoyette 				error = EINVAL;
    663   1.1  pgoyette 				break;
    664   1.1  pgoyette 			}
    665   1.1  pgoyette 			dst = kmem_alloc(len + 1, KM_SLEEP);
    666   1.1  pgoyette 			if ((error = copyin(path, dst, len)) != 0) {
    667   1.1  pgoyette 				kmem_free(dst, len + 1);
    668   1.1  pgoyette 				break;
    669   1.1  pgoyette 			}
    670   1.1  pgoyette 			path = dst;
    671   1.1  pgoyette 			path[len] = '\0';
    672   1.1  pgoyette 		}
    673   1.1  pgoyette 		error = (*coredump_vec)(lt, path);
    674   1.1  pgoyette 		if (path)
    675   1.1  pgoyette 			kmem_free(path, len + 1);
    676   1.1  pgoyette 		break;
    677   1.1  pgoyette 
    678   1.1  pgoyette #ifdef PT_STEP
    679   1.1  pgoyette 	case  PT_STEP:
    680   1.1  pgoyette 		/*
    681   1.1  pgoyette 		 * From the 4.4BSD PRM:
    682   1.1  pgoyette 		 * "Execution continues as in request PT_CONTINUE; however
    683   1.1  pgoyette 		 * as soon as possible after execution of at least one
    684   1.1  pgoyette 		 * instruction, execution stops again. [ ... ]"
    685   1.1  pgoyette 		 */
    686   1.1  pgoyette #endif
    687   1.1  pgoyette 	case  PT_CONTINUE:
    688   1.1  pgoyette 	case  PT_SYSCALL:
    689   1.1  pgoyette 	case  PT_DETACH:
    690   1.1  pgoyette 		if (req == PT_SYSCALL) {
    691   1.1  pgoyette 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
    692   1.1  pgoyette 				SET(t->p_slflag, PSL_SYSCALL);
    693   1.1  pgoyette #ifdef __HAVE_SYSCALL_INTERN
    694   1.1  pgoyette 				(*t->p_emul->e_syscall_intern)(t);
    695   1.1  pgoyette #endif
    696   1.1  pgoyette 			}
    697   1.1  pgoyette 		} else {
    698   1.1  pgoyette 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
    699   1.1  pgoyette 				CLR(t->p_slflag, PSL_SYSCALL);
    700   1.1  pgoyette #ifdef __HAVE_SYSCALL_INTERN
    701   1.1  pgoyette 				(*t->p_emul->e_syscall_intern)(t);
    702   1.1  pgoyette #endif
    703   1.1  pgoyette 			}
    704   1.1  pgoyette 		}
    705   1.1  pgoyette 		t->p_trace_enabled = trace_is_enabled(t);
    706   1.1  pgoyette 
    707   1.1  pgoyette 		/*
    708   1.1  pgoyette 		 * Pick up the LWPID, if supplied.  There are two cases:
    709   1.1  pgoyette 		 * data < 0 : step or continue single thread, lwp = -data
    710   1.1  pgoyette 		 * data > 0 in PT_STEP : step this thread, continue others
    711   1.1  pgoyette 		 * For operations other than PT_STEP, data > 0 means
    712   1.1  pgoyette 		 * data is the signo to deliver to the process.
    713   1.1  pgoyette 		 */
    714   1.1  pgoyette 		tmp = data;
    715   1.1  pgoyette 		if (tmp >= 0) {
    716   1.1  pgoyette #ifdef PT_STEP
    717   1.1  pgoyette 			if (req == PT_STEP)
    718   1.1  pgoyette 				signo = 0;
    719   1.1  pgoyette 			else
    720   1.1  pgoyette #endif
    721   1.1  pgoyette 			{
    722   1.1  pgoyette 				signo = tmp;
    723   1.1  pgoyette 				tmp = 0;	/* don't search for LWP */
    724   1.1  pgoyette 			}
    725   1.1  pgoyette 		} else
    726   1.1  pgoyette 			tmp = -tmp;
    727   1.1  pgoyette 
    728   1.1  pgoyette 		if (tmp > 0) {
    729   1.1  pgoyette 			if (req == PT_DETACH) {
    730   1.1  pgoyette 				error = EINVAL;
    731   1.1  pgoyette 				break;
    732   1.1  pgoyette 			}
    733   1.1  pgoyette 			lwp_delref2 (lt);
    734   1.1  pgoyette 			lt = lwp_find(t, tmp);
    735   1.1  pgoyette 			if (lt == NULL) {
    736   1.1  pgoyette 				error = ESRCH;
    737   1.1  pgoyette 				break;
    738   1.1  pgoyette 			}
    739   1.1  pgoyette 			lwp_addref(lt);
    740   1.1  pgoyette 			resume_all = 0;
    741   1.1  pgoyette 			signo = 0;
    742   1.1  pgoyette 		}
    743   1.1  pgoyette 
    744   1.1  pgoyette 		/*
    745   1.1  pgoyette 		 * From the 4.4BSD PRM:
    746   1.1  pgoyette 		 * "The data argument is taken as a signal number and the
    747   1.1  pgoyette 		 * child's execution continues at location addr as if it
    748   1.1  pgoyette 		 * incurred that signal.  Normally the signal number will
    749   1.1  pgoyette 		 * be either 0 to indicate that the signal that caused the
    750   1.1  pgoyette 		 * stop should be ignored, or that value fetched out of
    751   1.1  pgoyette 		 * the process's image indicating which signal caused
    752   1.1  pgoyette 		 * the stop.  If addr is (int *)1 then execution continues
    753   1.1  pgoyette 		 * from where it stopped."
    754   1.1  pgoyette 		 */
    755   1.1  pgoyette 
    756   1.1  pgoyette 		/* Check that the data is a valid signal number or zero. */
    757   1.1  pgoyette 		if (signo < 0 || signo >= NSIG) {
    758   1.1  pgoyette 			error = EINVAL;
    759   1.1  pgoyette 			break;
    760   1.1  pgoyette 		}
    761   1.1  pgoyette 
    762  1.15     kamil 		/* Prevent process deadlock */
    763  1.15     kamil 		if (resume_all) {
    764  1.15     kamil #ifdef PT_STEP
    765  1.15     kamil 			if (req == PT_STEP) {
    766  1.15     kamil 				if (lt->l_flag & LW_WSUSPEND) {
    767  1.15     kamil 					error = EDEADLK;
    768  1.15     kamil 					break;
    769  1.15     kamil 				}
    770  1.15     kamil 			} else
    771  1.15     kamil #endif
    772  1.15     kamil 			{
    773  1.15     kamil 				error = EDEADLK;
    774  1.15     kamil 				LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    775  1.15     kamil 					if ((lt2->l_flag & LW_WSUSPEND) == 0) {
    776  1.15     kamil 						error = 0;
    777  1.15     kamil 						break;
    778  1.15     kamil 					}
    779  1.15     kamil 				}
    780  1.15     kamil 				if (error != 0)
    781  1.15     kamil 					break;
    782  1.15     kamil 			}
    783  1.15     kamil 		} else {
    784  1.15     kamil 			if (lt->l_flag & LW_WSUSPEND) {
    785  1.15     kamil 				error = EDEADLK;
    786  1.15     kamil 				break;
    787  1.15     kamil 			}
    788  1.15     kamil 		}
    789  1.15     kamil 
    790   1.1  pgoyette 		/* If the address parameter is not (int *)1, set the pc. */
    791   1.1  pgoyette 		if ((int *)addr != (int *)1) {
    792   1.1  pgoyette 			error = process_set_pc(lt, addr);
    793   1.1  pgoyette 			if (error != 0)
    794   1.1  pgoyette 				break;
    795   1.1  pgoyette 		}
    796   1.1  pgoyette #ifdef PT_STEP
    797   1.1  pgoyette 		/*
    798   1.1  pgoyette 		 * Arrange for a single-step, if that's requested and possible.
    799   1.1  pgoyette 		 * More precisely, set the single step status as requested for
    800   1.1  pgoyette 		 * the requested thread, and clear it for other threads.
    801   1.1  pgoyette 		 */
    802   1.1  pgoyette 		LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    803   1.1  pgoyette 			if (lt != lt2) {
    804   1.1  pgoyette 				lwp_lock(lt2);
    805   1.1  pgoyette 				process_sstep(lt2, 0);
    806   1.1  pgoyette 				lwp_unlock(lt2);
    807   1.1  pgoyette 			}
    808   1.1  pgoyette 		}
    809   1.1  pgoyette 		error = process_sstep(lt, req == PT_STEP);
    810   1.1  pgoyette 		if (error)
    811   1.1  pgoyette 			break;
    812   1.1  pgoyette #endif
    813   1.1  pgoyette 		if (req == PT_DETACH) {
    814   1.1  pgoyette 			CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
    815   1.1  pgoyette 
    816   1.1  pgoyette 			/* give process back to original parent or init */
    817   1.1  pgoyette 			if (t->p_opptr != t->p_pptr) {
    818   1.1  pgoyette 				struct proc *pp = t->p_opptr;
    819   1.1  pgoyette 				proc_reparent(t, pp ? pp : initproc);
    820   1.1  pgoyette 			}
    821   1.1  pgoyette 
    822   1.1  pgoyette 			/* not being traced any more */
    823   1.1  pgoyette 			t->p_opptr = NULL;
    824   1.1  pgoyette 		}
    825   1.1  pgoyette 	sendsig:
    826   1.1  pgoyette 		t->p_fpid = 0;
    827   1.9     kamil 		t->p_vfpid = 0;
    828   1.9     kamil 		t->p_vfpid_done = 0;
    829  1.10     kamil 		t->p_lwp_created = 0;
    830  1.10     kamil 		t->p_lwp_exited = 0;
    831   1.1  pgoyette 		/* Finally, deliver the requested signal (or none). */
    832   1.1  pgoyette 		if (t->p_stat == SSTOP) {
    833   1.1  pgoyette 			/*
    834   1.1  pgoyette 			 * Unstop the process.  If it needs to take a
    835   1.1  pgoyette 			 * signal, make all efforts to ensure that at
    836   1.1  pgoyette 			 * an LWP runs to see it.
    837   1.1  pgoyette 			 */
    838   1.1  pgoyette 			t->p_xsig = signo;
    839   1.1  pgoyette 			if (resume_all)
    840   1.1  pgoyette 				proc_unstop(t);
    841   1.1  pgoyette 			else
    842   1.1  pgoyette 				lwp_unstop(lt);
    843   1.8     kamil 		} else if (t->p_sigctx.ps_faked) {
    844   1.8     kamil 			if (signo != t->p_sigctx.ps_info._signo) {
    845   1.8     kamil 				error = EINVAL;
    846   1.8     kamil 				break;
    847   1.8     kamil 			}
    848   1.8     kamil 			t->p_sigctx.ps_faked = false;
    849   1.8     kamil 			KSI_INIT_EMPTY(&ksi);
    850   1.8     kamil 			ksi.ksi_info = t->p_sigctx.ps_info;
    851   1.8     kamil 			ksi.ksi_lid = t->p_sigctx.ps_lwp;
    852   1.8     kamil 			kpsignal2(t, &ksi);
    853   1.1  pgoyette 		} else if (signo != 0) {
    854   1.1  pgoyette 			KSI_INIT_EMPTY(&ksi);
    855   1.1  pgoyette 			ksi.ksi_signo = signo;
    856   1.1  pgoyette 			kpsignal2(t, &ksi);
    857   1.1  pgoyette 		}
    858   1.1  pgoyette 		break;
    859   1.1  pgoyette 
    860   1.1  pgoyette 	case  PT_SYSCALLEMU:
    861   1.1  pgoyette 		if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
    862   1.1  pgoyette 			error = EINVAL;
    863   1.1  pgoyette 			break;
    864   1.1  pgoyette 		}
    865   1.1  pgoyette 		SET(t->p_slflag, PSL_SYSCALLEMU);
    866   1.1  pgoyette 		break;
    867   1.1  pgoyette 
    868   1.1  pgoyette 	case  PT_KILL:
    869   1.1  pgoyette 		/* just send the process a KILL signal. */
    870   1.1  pgoyette 		signo = SIGKILL;
    871   1.1  pgoyette 		goto sendsig;	/* in PT_CONTINUE, above. */
    872   1.1  pgoyette 
    873   1.1  pgoyette 	case  PT_ATTACH:
    874   1.1  pgoyette 		/*
    875   1.1  pgoyette 		 * Go ahead and set the trace flag.
    876   1.1  pgoyette 		 * Save the old parent (it's reset in
    877   1.1  pgoyette 		 *   _DETACH, and also in kern_exit.c:wait4()
    878   1.1  pgoyette 		 * Reparent the process so that the tracing
    879   1.1  pgoyette 		 *   proc gets to see all the action.
    880   1.1  pgoyette 		 * Stop the target.
    881   1.1  pgoyette 		 */
    882   1.2  christos 		proc_changeparent(t, p);
    883   1.4  christos 		signo = SIGSTOP;
    884   1.1  pgoyette 		goto sendsig;
    885   1.1  pgoyette 
    886   1.1  pgoyette 	case  PT_GET_EVENT_MASK:
    887   1.1  pgoyette 		if (data != sizeof(pe)) {
    888   1.1  pgoyette 			DPRINTF(("ptrace(%d): %d != %zu\n", req,
    889   1.1  pgoyette 			    data, sizeof(pe)));
    890   1.1  pgoyette 			error = EINVAL;
    891   1.1  pgoyette 			break;
    892   1.1  pgoyette 		}
    893   1.1  pgoyette 		memset(&pe, 0, sizeof(pe));
    894   1.1  pgoyette 		pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
    895   1.1  pgoyette 		    PTRACE_FORK : 0;
    896   1.9     kamil 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
    897   1.9     kamil 		    PTRACE_VFORK : 0;
    898   1.9     kamil 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
    899   1.9     kamil 		    PTRACE_VFORK_DONE : 0;
    900  1.10     kamil 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
    901  1.10     kamil 		    PTRACE_LWP_CREATE : 0;
    902  1.10     kamil 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
    903  1.10     kamil 		    PTRACE_LWP_EXIT : 0;
    904   1.1  pgoyette 		error = copyout(&pe, addr, sizeof(pe));
    905   1.1  pgoyette 		break;
    906   1.1  pgoyette 
    907   1.1  pgoyette 	case  PT_SET_EVENT_MASK:
    908   1.1  pgoyette 		if (data != sizeof(pe)) {
    909   1.1  pgoyette 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    910   1.1  pgoyette 			    sizeof(pe)));
    911   1.1  pgoyette 			error = EINVAL;
    912   1.1  pgoyette 			break;
    913   1.1  pgoyette 		}
    914   1.1  pgoyette 		if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
    915   1.1  pgoyette 			return error;
    916   1.1  pgoyette 		if (pe.pe_set_event & PTRACE_FORK)
    917   1.1  pgoyette 			SET(t->p_slflag, PSL_TRACEFORK);
    918   1.1  pgoyette 		else
    919   1.1  pgoyette 			CLR(t->p_slflag, PSL_TRACEFORK);
    920   1.9     kamil #if notyet
    921   1.9     kamil 		if (pe.pe_set_event & PTRACE_VFORK)
    922   1.9     kamil 			SET(t->p_slflag, PSL_TRACEVFORK);
    923   1.9     kamil 		else
    924   1.9     kamil 			CLR(t->p_slflag, PSL_TRACEVFORK);
    925   1.9     kamil #else
    926   1.9     kamil 		if (pe.pe_set_event & PTRACE_VFORK) {
    927   1.9     kamil 			error = ENOTSUP;
    928   1.9     kamil 			break;
    929   1.9     kamil 		}
    930   1.9     kamil #endif
    931   1.9     kamil 		if (pe.pe_set_event & PTRACE_VFORK_DONE)
    932   1.9     kamil 			SET(t->p_slflag, PSL_TRACEVFORK_DONE);
    933   1.9     kamil 		else
    934   1.9     kamil 			CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
    935  1.10     kamil 		if (pe.pe_set_event & PTRACE_LWP_CREATE)
    936  1.10     kamil 			SET(t->p_slflag, PSL_TRACELWP_CREATE);
    937  1.10     kamil 		else
    938  1.10     kamil 			CLR(t->p_slflag, PSL_TRACELWP_CREATE);
    939  1.10     kamil 		if (pe.pe_set_event & PTRACE_LWP_EXIT)
    940  1.10     kamil 			SET(t->p_slflag, PSL_TRACELWP_EXIT);
    941  1.10     kamil 		else
    942  1.10     kamil 			CLR(t->p_slflag, PSL_TRACELWP_EXIT);
    943   1.1  pgoyette 		break;
    944   1.1  pgoyette 
    945   1.1  pgoyette 	case  PT_GET_PROCESS_STATE:
    946   1.1  pgoyette 		if (data != sizeof(ps)) {
    947   1.1  pgoyette 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    948   1.1  pgoyette 			    sizeof(ps)));
    949   1.1  pgoyette 			error = EINVAL;
    950   1.1  pgoyette 			break;
    951   1.1  pgoyette 		}
    952   1.1  pgoyette 		memset(&ps, 0, sizeof(ps));
    953   1.1  pgoyette 		if (t->p_fpid) {
    954   1.1  pgoyette 			ps.pe_report_event = PTRACE_FORK;
    955   1.1  pgoyette 			ps.pe_other_pid = t->p_fpid;
    956   1.9     kamil 		} else if (t->p_vfpid) {
    957   1.9     kamil 			ps.pe_report_event = PTRACE_VFORK;
    958   1.9     kamil 			ps.pe_other_pid = t->p_vfpid;
    959   1.9     kamil 		} else if (t->p_vfpid_done) {
    960   1.9     kamil 			ps.pe_report_event = PTRACE_VFORK_DONE;
    961   1.9     kamil 			ps.pe_other_pid = t->p_vfpid_done;
    962  1.10     kamil 		} else if (t->p_lwp_created) {
    963  1.10     kamil 			ps.pe_report_event = PTRACE_LWP_CREATE;
    964  1.10     kamil 			ps.pe_lwp = t->p_lwp_created;
    965  1.10     kamil 		} else if (t->p_lwp_exited) {
    966  1.10     kamil 			ps.pe_report_event = PTRACE_LWP_EXIT;
    967  1.10     kamil 			ps.pe_lwp = t->p_lwp_exited;
    968   1.1  pgoyette 		}
    969   1.1  pgoyette 		error = copyout(&ps, addr, sizeof(ps));
    970   1.1  pgoyette 		break;
    971   1.1  pgoyette 
    972   1.1  pgoyette 	case PT_LWPINFO:
    973   1.1  pgoyette 		if (data != sizeof(pl)) {
    974   1.1  pgoyette 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    975   1.1  pgoyette 			    sizeof(pl)));
    976   1.1  pgoyette 			error = EINVAL;
    977   1.1  pgoyette 			break;
    978   1.1  pgoyette 		}
    979   1.1  pgoyette 		error = copyin(addr, &pl, sizeof(pl));
    980   1.1  pgoyette 		if (error)
    981   1.1  pgoyette 			break;
    982   1.1  pgoyette 		tmp = pl.pl_lwpid;
    983   1.1  pgoyette 		lwp_delref(lt);
    984   1.1  pgoyette 		mutex_enter(t->p_lock);
    985   1.1  pgoyette 		if (tmp == 0)
    986   1.1  pgoyette 			lt = lwp_find_first(t);
    987   1.1  pgoyette 		else {
    988   1.1  pgoyette 			lt = lwp_find(t, tmp);
    989   1.1  pgoyette 			if (lt == NULL) {
    990   1.1  pgoyette 				mutex_exit(t->p_lock);
    991   1.1  pgoyette 				error = ESRCH;
    992   1.1  pgoyette 				break;
    993   1.1  pgoyette 			}
    994   1.1  pgoyette 			lt = LIST_NEXT(lt, l_sibling);
    995   1.1  pgoyette 		}
    996   1.1  pgoyette 		while (lt != NULL && !lwp_alive(lt))
    997   1.1  pgoyette 			lt = LIST_NEXT(lt, l_sibling);
    998   1.1  pgoyette 		pl.pl_lwpid = 0;
    999   1.1  pgoyette 		pl.pl_event = 0;
   1000   1.1  pgoyette 		if (lt) {
   1001   1.1  pgoyette 			lwp_addref(lt);
   1002   1.1  pgoyette 			pl.pl_lwpid = lt->l_lid;
   1003  1.15     kamil 
   1004  1.15     kamil 			if (lt->l_flag & LW_WSUSPEND)
   1005  1.15     kamil 				pl.pl_event = PL_EVENT_SUSPENDED;
   1006   1.6  christos 			/*
   1007   1.6  christos 			 * If we match the lwp, or it was sent to every lwp,
   1008   1.6  christos 			 * we set PL_EVENT_SIGNAL.
   1009   1.6  christos 			 * XXX: ps_lwp == 0 means everyone and noone, so
   1010   1.6  christos 			 * check ps_signo too.
   1011   1.6  christos 			 */
   1012  1.15     kamil 			else if (lt->l_lid == t->p_sigctx.ps_lwp
   1013  1.15     kamil 			         || (t->p_sigctx.ps_lwp == 0 &&
   1014  1.15     kamil 			             t->p_sigctx.ps_info._signo))
   1015   1.1  pgoyette 				pl.pl_event = PL_EVENT_SIGNAL;
   1016   1.1  pgoyette 		}
   1017   1.1  pgoyette 		mutex_exit(t->p_lock);
   1018   1.1  pgoyette 
   1019   1.1  pgoyette 		error = copyout(&pl, addr, sizeof(pl));
   1020   1.1  pgoyette 		break;
   1021   1.1  pgoyette 
   1022   1.8     kamil 	case  PT_SET_SIGINFO:
   1023   1.8     kamil 		if (data != sizeof(psi)) {
   1024   1.8     kamil 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1025   1.8     kamil 			    sizeof(psi)));
   1026   1.8     kamil 			error = EINVAL;
   1027   1.8     kamil 			break;
   1028   1.8     kamil 		}
   1029   1.8     kamil 
   1030   1.8     kamil 		error = copyin(addr, &psi, sizeof(psi));
   1031   1.8     kamil 		if (error)
   1032   1.8     kamil 			break;
   1033   1.8     kamil 
   1034   1.8     kamil 		/* Check that the data is a valid signal number or zero. */
   1035   1.8     kamil 		if (psi.psi_siginfo.si_signo < 0 ||
   1036   1.8     kamil 		    psi.psi_siginfo.si_signo >= NSIG) {
   1037   1.8     kamil 			error = EINVAL;
   1038   1.8     kamil 			break;
   1039   1.8     kamil 		}
   1040   1.8     kamil 
   1041   1.8     kamil 		tmp = psi.psi_lwpid;
   1042   1.8     kamil 		if (tmp != 0)
   1043   1.8     kamil 			lwp_delref(lt);
   1044   1.8     kamil 
   1045   1.8     kamil 		mutex_enter(t->p_lock);
   1046   1.8     kamil 
   1047   1.8     kamil 		if (tmp != 0) {
   1048   1.8     kamil 			lt = lwp_find(t, tmp);
   1049   1.8     kamil 			if (lt == NULL) {
   1050   1.8     kamil 				mutex_exit(t->p_lock);
   1051   1.8     kamil 				error = ESRCH;
   1052   1.8     kamil 				break;
   1053   1.8     kamil 			}
   1054   1.8     kamil 			lwp_addref(lt);
   1055   1.8     kamil 		}
   1056   1.8     kamil 
   1057   1.8     kamil 		t->p_sigctx.ps_faked = true;
   1058   1.8     kamil 		t->p_sigctx.ps_info = psi.psi_siginfo._info;
   1059   1.8     kamil 		t->p_sigctx.ps_lwp = psi.psi_lwpid;
   1060   1.8     kamil 		mutex_exit(t->p_lock);
   1061   1.8     kamil 		break;
   1062   1.8     kamil 
   1063   1.8     kamil 	case  PT_GET_SIGINFO:
   1064   1.8     kamil 		if (data != sizeof(psi)) {
   1065   1.8     kamil 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1066   1.8     kamil 			    sizeof(psi)));
   1067   1.8     kamil 			error = EINVAL;
   1068   1.8     kamil 			break;
   1069   1.8     kamil 		}
   1070   1.8     kamil 		mutex_enter(t->p_lock);
   1071   1.8     kamil 		psi.psi_siginfo._info = t->p_sigctx.ps_info;
   1072   1.8     kamil 		psi.psi_lwpid = t->p_sigctx.ps_lwp;
   1073   1.8     kamil 		mutex_exit(t->p_lock);
   1074   1.8     kamil 
   1075   1.8     kamil 		error = copyout(&psi, addr, sizeof(psi));
   1076   1.8     kamil 		if (error)
   1077   1.8     kamil 			break;
   1078   1.8     kamil 
   1079   1.8     kamil 		break;
   1080   1.8     kamil 
   1081  1.14     kamil 	case  PT_SET_SIGMASK:
   1082  1.14     kamil 		write = 1;
   1083  1.14     kamil 
   1084  1.14     kamil 	case  PT_GET_SIGMASK:
   1085  1.14     kamil 		/* write = 0 done above. */
   1086  1.14     kamil 
   1087  1.14     kamil 		tmp = data;
   1088  1.14     kamil 		if (tmp != 0 && t->p_nlwps > 1) {
   1089  1.14     kamil 			lwp_delref(lt);
   1090  1.14     kamil 			mutex_enter(t->p_lock);
   1091  1.14     kamil 			lt = lwp_find(t, tmp);
   1092  1.14     kamil 			if (lt == NULL) {
   1093  1.14     kamil 				mutex_exit(t->p_lock);
   1094  1.14     kamil 				error = ESRCH;
   1095  1.14     kamil 				break;
   1096  1.14     kamil 			}
   1097  1.14     kamil 			lwp_addref(lt);
   1098  1.14     kamil 			mutex_exit(t->p_lock);
   1099  1.14     kamil 		}
   1100  1.14     kamil 
   1101  1.14     kamil 		if (!process_validregs(lt))
   1102  1.14     kamil 			error = EINVAL;
   1103  1.14     kamil 		else if (write == 1)
   1104  1.14     kamil 			error = copyin(addr, &lt->l_sigmask, sizeof(sigset_t));
   1105  1.14     kamil 		else
   1106  1.14     kamil 			error = copyout(&lt->l_sigmask, addr, sizeof(sigset_t));
   1107  1.14     kamil 
   1108  1.14     kamil 		break;
   1109  1.14     kamil 
   1110  1.15     kamil 	case  PT_RESUME:
   1111  1.15     kamil 		write = 1;
   1112  1.15     kamil 
   1113  1.15     kamil 	case  PT_SUSPEND:
   1114  1.15     kamil 		/* write = 0 done above. */
   1115  1.15     kamil 
   1116  1.15     kamil 		tmp = data;
   1117  1.15     kamil 		if (tmp != 0 && t->p_nlwps > 1) {
   1118  1.15     kamil 			lwp_delref(lt);
   1119  1.15     kamil 			mutex_enter(t->p_lock);
   1120  1.15     kamil 			lt = lwp_find(t, tmp);
   1121  1.15     kamil 			if (lt == NULL) {
   1122  1.15     kamil 				mutex_exit(t->p_lock);
   1123  1.15     kamil 				error = ESRCH;
   1124  1.15     kamil 				break;
   1125  1.15     kamil 			}
   1126  1.15     kamil 			lwp_addref(lt);
   1127  1.15     kamil 			mutex_exit(t->p_lock);
   1128  1.15     kamil 		}
   1129  1.15     kamil 		if (lt->l_flag & LW_SYSTEM) {
   1130  1.15     kamil 			error = EINVAL;
   1131  1.15     kamil 		} else {
   1132  1.15     kamil 			lwp_lock(lt);
   1133  1.15     kamil 			if (write == 0)
   1134  1.15     kamil 				lt->l_flag |= LW_WSUSPEND;
   1135  1.15     kamil 			else
   1136  1.15     kamil 				lt->l_flag &= ~LW_WSUSPEND;
   1137  1.15     kamil 			lwp_unlock(lt);
   1138  1.15     kamil 		}
   1139  1.15     kamil 		break;
   1140  1.15     kamil 
   1141   1.1  pgoyette #ifdef PT_SETREGS
   1142   1.1  pgoyette 	case  PT_SETREGS:
   1143   1.1  pgoyette 		write = 1;
   1144   1.1  pgoyette #endif
   1145   1.1  pgoyette #ifdef PT_GETREGS
   1146   1.1  pgoyette 	case  PT_GETREGS:
   1147   1.1  pgoyette 		/* write = 0 done above. */
   1148   1.1  pgoyette #endif
   1149   1.1  pgoyette #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1150   1.1  pgoyette 		tmp = data;
   1151   1.1  pgoyette 		if (tmp != 0 && t->p_nlwps > 1) {
   1152   1.1  pgoyette 			lwp_delref(lt);
   1153   1.1  pgoyette 			mutex_enter(t->p_lock);
   1154   1.1  pgoyette 			lt = lwp_find(t, tmp);
   1155   1.1  pgoyette 			if (lt == NULL) {
   1156   1.1  pgoyette 				mutex_exit(t->p_lock);
   1157   1.1  pgoyette 				error = ESRCH;
   1158   1.1  pgoyette 				break;
   1159   1.1  pgoyette 			}
   1160   1.1  pgoyette 			lwp_addref(lt);
   1161   1.1  pgoyette 			mutex_exit(t->p_lock);
   1162   1.1  pgoyette 		}
   1163   1.1  pgoyette 		if (!process_validregs(lt))
   1164   1.1  pgoyette 			error = EINVAL;
   1165   1.1  pgoyette 		else {
   1166   1.1  pgoyette 			error = proc_vmspace_getref(p, &vm);
   1167   1.1  pgoyette 			if (error)
   1168   1.1  pgoyette 				break;
   1169   1.1  pgoyette 			iov.iov_base = addr;
   1170   1.1  pgoyette 			iov.iov_len = PROC_REGSZ(p);
   1171   1.1  pgoyette 			uio.uio_iov = &iov;
   1172   1.1  pgoyette 			uio.uio_iovcnt = 1;
   1173   1.1  pgoyette 			uio.uio_offset = 0;
   1174   1.1  pgoyette 			uio.uio_resid = iov.iov_len;
   1175   1.1  pgoyette 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1176   1.1  pgoyette 			uio.uio_vmspace = vm;
   1177   1.1  pgoyette 
   1178   1.1  pgoyette 			error = ptm->ptm_doregs(l, lt, &uio);
   1179   1.1  pgoyette 			uvmspace_free(vm);
   1180   1.1  pgoyette 		}
   1181   1.1  pgoyette 		break;
   1182   1.1  pgoyette #endif
   1183   1.1  pgoyette 
   1184   1.1  pgoyette #ifdef PT_SETFPREGS
   1185   1.1  pgoyette 	case  PT_SETFPREGS:
   1186   1.1  pgoyette 		write = 1;
   1187   1.5  christos 		/*FALLTHROUGH*/
   1188   1.1  pgoyette #endif
   1189   1.1  pgoyette #ifdef PT_GETFPREGS
   1190   1.1  pgoyette 	case  PT_GETFPREGS:
   1191   1.1  pgoyette 		/* write = 0 done above. */
   1192   1.1  pgoyette #endif
   1193   1.1  pgoyette #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1194   1.1  pgoyette 		tmp = data;
   1195   1.1  pgoyette 		if (tmp != 0 && t->p_nlwps > 1) {
   1196   1.1  pgoyette 			lwp_delref(lt);
   1197   1.1  pgoyette 			mutex_enter(t->p_lock);
   1198   1.1  pgoyette 			lt = lwp_find(t, tmp);
   1199   1.1  pgoyette 			if (lt == NULL) {
   1200   1.1  pgoyette 				mutex_exit(t->p_lock);
   1201   1.1  pgoyette 				error = ESRCH;
   1202   1.1  pgoyette 				break;
   1203   1.1  pgoyette 			}
   1204   1.1  pgoyette 			lwp_addref(lt);
   1205   1.1  pgoyette 			mutex_exit(t->p_lock);
   1206   1.1  pgoyette 		}
   1207   1.1  pgoyette 		if (!process_validfpregs(lt))
   1208   1.1  pgoyette 			error = EINVAL;
   1209   1.1  pgoyette 		else {
   1210   1.1  pgoyette 			error = proc_vmspace_getref(p, &vm);
   1211   1.1  pgoyette 			if (error)
   1212   1.1  pgoyette 				break;
   1213   1.1  pgoyette 			iov.iov_base = addr;
   1214   1.1  pgoyette 			iov.iov_len = PROC_FPREGSZ(p);
   1215   1.1  pgoyette 			uio.uio_iov = &iov;
   1216   1.1  pgoyette 			uio.uio_iovcnt = 1;
   1217   1.1  pgoyette 			uio.uio_offset = 0;
   1218   1.1  pgoyette 			uio.uio_resid = iov.iov_len;
   1219   1.1  pgoyette 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1220   1.1  pgoyette 			uio.uio_vmspace = vm;
   1221   1.1  pgoyette 
   1222   1.1  pgoyette 			error = ptm->ptm_dofpregs(l, lt, &uio);
   1223   1.1  pgoyette 			uvmspace_free(vm);
   1224   1.1  pgoyette 		}
   1225   1.1  pgoyette 		break;
   1226   1.1  pgoyette #endif
   1227   1.1  pgoyette 
   1228   1.7     kamil #ifdef __HAVE_PTRACE_WATCHPOINTS
   1229   1.7     kamil 		/*
   1230   1.7     kamil 		 * The "write" variable is used as type of operation.
   1231   1.7     kamil 		 * Possible values:
   1232   1.7     kamil 		 *	0 - return the number of supported hardware watchpoints
   1233   1.7     kamil 		 *	1 - set new watchpoint value
   1234   1.7     kamil 		 *	2 - get existing watchpoint image
   1235   1.7     kamil 		 */
   1236   1.7     kamil 	case  PT_WRITE_WATCHPOINT:
   1237   1.7     kamil 		write = 1;
   1238   1.7     kamil 	case  PT_READ_WATCHPOINT:
   1239   1.7     kamil 		/* write = 0 done above */
   1240   1.7     kamil 
   1241   1.7     kamil 		if (data != sizeof(pw)) {
   1242   1.7     kamil 			DPRINTF(("ptrace(%d): %d != %zu\n", req,
   1243   1.7     kamil 			    data, sizeof(pe)));
   1244   1.7     kamil 			error = EINVAL;
   1245   1.7     kamil 			break;
   1246   1.7     kamil 		}
   1247   1.7     kamil 		error = copyin(addr, &pw, sizeof(pw));
   1248   1.7     kamil 		if (error)
   1249   1.7     kamil 			break;
   1250   1.7     kamil 		tmp = pw.pw_lwpid;
   1251   1.7     kamil 		if (tmp != 0 && t->p_nlwps > 1) {
   1252   1.7     kamil 			lwp_delref(lt);
   1253   1.7     kamil 			mutex_enter(t->p_lock);
   1254   1.7     kamil 			lt = lwp_find(t, tmp);
   1255   1.7     kamil 			if (lt == NULL) {
   1256   1.7     kamil 				mutex_exit(t->p_lock);
   1257   1.7     kamil 				error = ESRCH;
   1258   1.7     kamil 				break;
   1259   1.7     kamil 			}
   1260   1.7     kamil 			lwp_addref(lt);
   1261   1.7     kamil 			mutex_exit(t->p_lock);
   1262   1.7     kamil 		}
   1263   1.7     kamil 		++write;
   1264   1.7     kamil 	case  PT_COUNT_WATCHPOINTS:
   1265   1.7     kamil 		if (!process_validwatchpoint(lt))
   1266   1.7     kamil 			error = EINVAL;
   1267   1.7     kamil 		else {
   1268   1.7     kamil 			lwp_lock(lt);
   1269   1.7     kamil 			error = ptm->ptm_dowatchpoint(l, lt, write, &pw, addr,
   1270   1.7     kamil 			    retval);
   1271   1.7     kamil 			lwp_unlock(lt);
   1272   1.7     kamil 		}
   1273   1.7     kamil 		break;
   1274   1.7     kamil #endif
   1275   1.7     kamil 
   1276   1.1  pgoyette #ifdef __HAVE_PTRACE_MACHDEP
   1277   1.1  pgoyette 	PTRACE_MACHDEP_REQUEST_CASES
   1278   1.1  pgoyette 		error = ptrace_machdep_dorequest(l, lt, req, addr, data);
   1279   1.1  pgoyette 		break;
   1280   1.1  pgoyette #endif
   1281   1.1  pgoyette 	}
   1282   1.1  pgoyette 
   1283   1.1  pgoyette 	if (pheld) {
   1284   1.1  pgoyette 		mutex_exit(t->p_lock);
   1285   1.1  pgoyette 		mutex_exit(proc_lock);
   1286   1.1  pgoyette 	}
   1287   1.1  pgoyette 	if (lt != NULL)
   1288   1.1  pgoyette 		lwp_delref(lt);
   1289   1.1  pgoyette 	rw_exit(&t->p_reflock);
   1290   1.1  pgoyette 
   1291   1.1  pgoyette 	return error;
   1292   1.1  pgoyette }
   1293   1.1  pgoyette 
   1294   1.1  pgoyette int
   1295   1.1  pgoyette process_doregs(struct lwp *curl /*tracer*/,
   1296   1.1  pgoyette     struct lwp *l /*traced*/,
   1297   1.1  pgoyette     struct uio *uio)
   1298   1.1  pgoyette {
   1299   1.1  pgoyette #if defined(PT_GETREGS) || defined(PT_SETREGS)
   1300   1.1  pgoyette 	int error;
   1301   1.1  pgoyette 	struct reg r;
   1302   1.1  pgoyette 	char *kv;
   1303   1.1  pgoyette 	int kl;
   1304   1.1  pgoyette 
   1305   1.1  pgoyette 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1306   1.1  pgoyette 		return EINVAL;
   1307   1.1  pgoyette 
   1308   1.1  pgoyette 	kl = sizeof(r);
   1309   1.1  pgoyette 	kv = (char *)&r;
   1310   1.1  pgoyette 
   1311   1.1  pgoyette 	kv += uio->uio_offset;
   1312   1.1  pgoyette 	kl -= uio->uio_offset;
   1313   1.1  pgoyette 	if ((size_t)kl > uio->uio_resid)
   1314   1.1  pgoyette 		kl = uio->uio_resid;
   1315   1.1  pgoyette 
   1316   1.1  pgoyette 	error = process_read_regs(l, &r);
   1317   1.1  pgoyette 	if (error == 0)
   1318   1.1  pgoyette 		error = uiomove(kv, kl, uio);
   1319   1.1  pgoyette 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1320   1.1  pgoyette 		if (l->l_stat != LSSTOP)
   1321   1.1  pgoyette 			error = EBUSY;
   1322   1.1  pgoyette 		else
   1323   1.1  pgoyette 			error = process_write_regs(l, &r);
   1324   1.1  pgoyette 	}
   1325   1.1  pgoyette 
   1326   1.1  pgoyette 	uio->uio_offset = 0;
   1327   1.1  pgoyette 	return error;
   1328   1.1  pgoyette #else
   1329   1.1  pgoyette 	return EINVAL;
   1330   1.1  pgoyette #endif
   1331   1.1  pgoyette }
   1332   1.1  pgoyette 
   1333   1.1  pgoyette int
   1334   1.1  pgoyette process_validregs(struct lwp *l)
   1335   1.1  pgoyette {
   1336   1.1  pgoyette 
   1337   1.1  pgoyette #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1338   1.1  pgoyette 	return (l->l_flag & LW_SYSTEM) == 0;
   1339   1.1  pgoyette #else
   1340   1.1  pgoyette 	return 0;
   1341   1.1  pgoyette #endif
   1342   1.1  pgoyette }
   1343   1.1  pgoyette 
   1344   1.1  pgoyette int
   1345   1.1  pgoyette process_dofpregs(struct lwp *curl /*tracer*/,
   1346   1.1  pgoyette     struct lwp *l /*traced*/,
   1347   1.1  pgoyette     struct uio *uio)
   1348   1.1  pgoyette {
   1349   1.1  pgoyette #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
   1350   1.1  pgoyette 	int error;
   1351   1.1  pgoyette 	struct fpreg r;
   1352   1.1  pgoyette 	char *kv;
   1353   1.1  pgoyette 	size_t kl;
   1354   1.1  pgoyette 
   1355   1.1  pgoyette 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1356   1.1  pgoyette 		return EINVAL;
   1357   1.1  pgoyette 
   1358   1.1  pgoyette 	kl = sizeof(r);
   1359   1.1  pgoyette 	kv = (char *)&r;
   1360   1.1  pgoyette 
   1361   1.1  pgoyette 	kv += uio->uio_offset;
   1362   1.1  pgoyette 	kl -= uio->uio_offset;
   1363   1.1  pgoyette 	if (kl > uio->uio_resid)
   1364   1.1  pgoyette 		kl = uio->uio_resid;
   1365   1.1  pgoyette 
   1366   1.1  pgoyette 	error = process_read_fpregs(l, &r, &kl);
   1367   1.1  pgoyette 	if (error == 0)
   1368   1.1  pgoyette 		error = uiomove(kv, kl, uio);
   1369   1.1  pgoyette 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1370   1.1  pgoyette 		if (l->l_stat != LSSTOP)
   1371   1.1  pgoyette 			error = EBUSY;
   1372   1.1  pgoyette 		else
   1373   1.1  pgoyette 			error = process_write_fpregs(l, &r, kl);
   1374   1.1  pgoyette 	}
   1375   1.1  pgoyette 	uio->uio_offset = 0;
   1376   1.1  pgoyette 	return error;
   1377   1.1  pgoyette #else
   1378   1.1  pgoyette 	return EINVAL;
   1379   1.1  pgoyette #endif
   1380   1.1  pgoyette }
   1381   1.1  pgoyette 
   1382   1.1  pgoyette int
   1383   1.1  pgoyette process_validfpregs(struct lwp *l)
   1384   1.1  pgoyette {
   1385   1.1  pgoyette 
   1386   1.1  pgoyette #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1387   1.1  pgoyette 	return (l->l_flag & LW_SYSTEM) == 0;
   1388   1.1  pgoyette #else
   1389   1.1  pgoyette 	return 0;
   1390   1.1  pgoyette #endif
   1391   1.1  pgoyette }
   1392   1.1  pgoyette 
   1393   1.1  pgoyette static int
   1394   1.1  pgoyette process_auxv_offset(struct proc *p, struct uio *uio)
   1395   1.1  pgoyette {
   1396   1.1  pgoyette 	struct ps_strings pss;
   1397   1.1  pgoyette 	int error;
   1398   1.1  pgoyette 	off_t off = (off_t)p->p_psstrp;
   1399   1.1  pgoyette 
   1400   1.1  pgoyette 	if ((error = copyin_psstrings(p, &pss)) != 0)
   1401   1.1  pgoyette 		return error;
   1402   1.1  pgoyette 
   1403   1.1  pgoyette 	if (pss.ps_envstr == NULL)
   1404   1.1  pgoyette 		return EIO;
   1405   1.1  pgoyette 
   1406   1.1  pgoyette 	uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
   1407   1.1  pgoyette #ifdef __MACHINE_STACK_GROWS_UP
   1408   1.1  pgoyette 	if (uio->uio_offset < off)
   1409   1.1  pgoyette 		return EIO;
   1410   1.1  pgoyette #else
   1411   1.1  pgoyette 	if (uio->uio_offset > off)
   1412   1.1  pgoyette 		return EIO;
   1413   1.1  pgoyette 	if ((uio->uio_offset + uio->uio_resid) > off)
   1414   1.1  pgoyette 		uio->uio_resid = off - uio->uio_offset;
   1415   1.1  pgoyette #endif
   1416   1.1  pgoyette 	return 0;
   1417   1.1  pgoyette }
   1418   1.7     kamil 
   1419   1.7     kamil int
   1420   1.7     kamil process_dowatchpoint(struct lwp *curl /*tracer*/, struct lwp *l /*traced*/,
   1421   1.7     kamil     int operation, struct ptrace_watchpoint *pw, void *addr,
   1422   1.7     kamil     register_t *retval)
   1423   1.7     kamil {
   1424   1.7     kamil 
   1425   1.7     kamil #ifdef __HAVE_PTRACE_WATCHPOINTS
   1426   1.7     kamil 	int error;
   1427   1.7     kamil 
   1428   1.7     kamil 	KASSERT(operation >= 0);
   1429   1.7     kamil 	KASSERT(operation <= 2);
   1430   1.7     kamil 
   1431   1.7     kamil 	switch (operation) {
   1432   1.7     kamil 	case 0:
   1433   1.7     kamil 		return process_count_watchpoints(l, retval);
   1434   1.7     kamil 	case 1:
   1435   1.7     kamil 		error = process_read_watchpoint(l, pw);
   1436   1.7     kamil 		if (error)
   1437   1.7     kamil 			return error;
   1438   1.7     kamil 		return copyout(pw, addr, sizeof(*pw));
   1439   1.7     kamil 	default:
   1440   1.7     kamil 		return process_write_watchpoint(l, pw);
   1441   1.7     kamil 	}
   1442   1.7     kamil #else
   1443   1.7     kamil 	return EINVAL;
   1444   1.7     kamil #endif
   1445   1.7     kamil }
   1446   1.7     kamil 
   1447   1.7     kamil int
   1448   1.7     kamil process_validwatchpoint(struct lwp *l)
   1449   1.7     kamil {
   1450   1.7     kamil 
   1451   1.7     kamil #ifdef __HAVE_PTRACE_WATCHPOINTS
   1452   1.7     kamil 	return (l->l_flag & LW_SYSTEM) == 0;
   1453   1.7     kamil #else
   1454   1.7     kamil 	return 0;
   1455   1.7     kamil #endif
   1456   1.7     kamil }
   1457   1.1  pgoyette #endif /* PTRACE */
   1458   1.1  pgoyette 
   1459   1.1  pgoyette MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
   1460   1.1  pgoyette 
   1461   1.1  pgoyette static int
   1462   1.1  pgoyette ptrace_common_modcmd(modcmd_t cmd, void *arg)
   1463   1.1  pgoyette {
   1464   1.1  pgoyette         int error;
   1465   1.1  pgoyette 
   1466   1.1  pgoyette         switch (cmd) {
   1467   1.1  pgoyette         case MODULE_CMD_INIT:
   1468   1.1  pgoyette                 error = ptrace_init();
   1469   1.1  pgoyette                 break;
   1470   1.1  pgoyette         case MODULE_CMD_FINI:
   1471   1.1  pgoyette                 error = ptrace_fini();
   1472   1.1  pgoyette                 break;
   1473   1.1  pgoyette         default:
   1474   1.1  pgoyette 		ptrace_hooks();
   1475   1.1  pgoyette                 error = ENOTTY;
   1476   1.1  pgoyette                 break;
   1477   1.1  pgoyette         }
   1478   1.1  pgoyette         return error;
   1479   1.1  pgoyette }
   1480