Home | History | Annotate | Line # | Download | only in kern
sys_ptrace_common.c revision 1.45.2.2
      1  1.45.2.2    martin /*	$NetBSD: sys_ptrace_common.c,v 1.45.2.2 2020/04/13 08:05:04 martin 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.45.2.2    martin __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.45.2.2 2020/04/13 08:05:04 martin 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.24  christos #include "opt_compat_netbsd32.h"
    128       1.1  pgoyette #endif
    129       1.1  pgoyette 
    130      1.25  christos #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
    131      1.25  christos     && !defined(_RUMPKERNEL)
    132      1.25  christos #define COMPAT_NETBSD32
    133      1.25  christos #endif
    134      1.25  christos 
    135       1.1  pgoyette #include <sys/param.h>
    136       1.1  pgoyette #include <sys/systm.h>
    137       1.1  pgoyette #include <sys/proc.h>
    138       1.1  pgoyette #include <sys/errno.h>
    139       1.1  pgoyette #include <sys/exec.h>
    140       1.1  pgoyette #include <sys/pax.h>
    141       1.1  pgoyette #include <sys/ptrace.h>
    142       1.1  pgoyette #include <sys/uio.h>
    143       1.1  pgoyette #include <sys/ras.h>
    144       1.1  pgoyette #include <sys/kmem.h>
    145       1.1  pgoyette #include <sys/kauth.h>
    146       1.1  pgoyette #include <sys/mount.h>
    147       1.1  pgoyette #include <sys/syscallargs.h>
    148       1.1  pgoyette #include <sys/module.h>
    149       1.1  pgoyette #include <sys/condvar.h>
    150       1.1  pgoyette #include <sys/mutex.h>
    151  1.45.2.2    martin #include <sys/compat_stub.h>
    152       1.1  pgoyette 
    153       1.1  pgoyette #include <uvm/uvm_extern.h>
    154       1.1  pgoyette 
    155       1.1  pgoyette #include <machine/reg.h>
    156       1.1  pgoyette 
    157       1.1  pgoyette #ifdef PTRACE
    158      1.45  christos # ifdef PTRACE_DEBUG
    159       1.1  pgoyette #  define DPRINTF(a) uprintf a
    160       1.1  pgoyette # else
    161       1.1  pgoyette #  define DPRINTF(a)
    162       1.1  pgoyette # endif
    163       1.1  pgoyette 
    164       1.1  pgoyette static kauth_listener_t ptrace_listener;
    165       1.1  pgoyette static int process_auxv_offset(struct proc *, struct uio *);
    166       1.1  pgoyette 
    167  1.45.2.1  christos extern int user_va0_disable;
    168  1.45.2.1  christos 
    169       1.1  pgoyette #if 0
    170       1.1  pgoyette static int ptrace_cbref;
    171       1.1  pgoyette static kmutex_t ptrace_mtx;
    172       1.1  pgoyette static kcondvar_t ptrace_cv;
    173       1.1  pgoyette #endif
    174       1.1  pgoyette 
    175      1.28  christos #ifdef PT_GETREGS
    176      1.28  christos # define case_PT_GETREGS	case PT_GETREGS:
    177      1.28  christos #else
    178      1.28  christos # define case_PT_GETREGS
    179      1.28  christos #endif
    180      1.28  christos 
    181      1.28  christos #ifdef PT_SETREGS
    182      1.28  christos # define case_PT_SETREGS	case PT_SETREGS:
    183      1.28  christos #else
    184      1.28  christos # define case_PT_SETREGS
    185      1.28  christos #endif
    186      1.28  christos 
    187      1.28  christos #ifdef PT_GETFPREGS
    188      1.28  christos # define case_PT_GETFPREGS	case PT_GETFPREGS:
    189      1.28  christos #else
    190      1.28  christos # define case_PT_GETFPREGS
    191      1.28  christos #endif
    192      1.28  christos 
    193      1.28  christos #ifdef PT_SETFPREGS
    194      1.28  christos # define case_PT_SETFPREGS	case PT_SETFPREGS:
    195      1.28  christos #else
    196      1.28  christos # define case_PT_SETFPREGS
    197      1.28  christos #endif
    198      1.28  christos 
    199      1.28  christos #ifdef PT_GETDBREGS
    200      1.28  christos # define case_PT_GETDBREGS	case PT_GETDBREGS:
    201      1.28  christos #else
    202      1.28  christos # define case_PT_GETDBREGS
    203      1.28  christos #endif
    204      1.28  christos 
    205      1.28  christos #ifdef PT_SETDBREGS
    206      1.28  christos # define case_PT_SETDBREGS	case PT_SETDBREGS:
    207      1.28  christos #else
    208      1.28  christos # define case_PT_SETDBREGS
    209      1.28  christos #endif
    210      1.28  christos 
    211      1.28  christos #if defined(PT_SETREGS) || defined(PT_GETREGS) || \
    212  1.45.2.1  christos     defined(PT_SETFPREGS) || defined(PT_GETFPREGS) || \
    213      1.28  christos     defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
    214      1.28  christos # define PT_REGISTERS
    215      1.28  christos #endif
    216      1.28  christos 
    217       1.1  pgoyette static int
    218       1.1  pgoyette ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    219       1.1  pgoyette     void *arg0, void *arg1, void *arg2, void *arg3)
    220       1.1  pgoyette {
    221       1.1  pgoyette 	struct proc *p;
    222       1.1  pgoyette 	int result;
    223      1.36     kamil #ifdef PT_SETDBREGS
    224      1.36     kamil 	extern int user_set_dbregs;
    225      1.36     kamil #endif
    226       1.1  pgoyette 
    227       1.1  pgoyette 	result = KAUTH_RESULT_DEFER;
    228       1.1  pgoyette 	p = arg0;
    229       1.1  pgoyette 
    230       1.1  pgoyette #if 0
    231       1.1  pgoyette 	mutex_enter(&ptrace_mtx);
    232       1.1  pgoyette 	ptrace_cbref++;
    233       1.1  pgoyette 	mutex_exit(&ptrace_mtx);
    234       1.1  pgoyette #endif
    235       1.1  pgoyette 	if (action != KAUTH_PROCESS_PTRACE)
    236       1.1  pgoyette 		goto out;
    237       1.1  pgoyette 
    238       1.1  pgoyette 	switch ((u_long)arg1) {
    239      1.36     kamil #ifdef PT_SETDBREGS
    240      1.36     kamil 	case_PT_SETDBREGS
    241      1.36     kamil 		if (kauth_cred_getuid(cred) != 0 && user_set_dbregs == 0) {
    242      1.36     kamil 			result = KAUTH_RESULT_DENY;
    243      1.36     kamil 			break;
    244      1.36     kamil 		}
    245      1.36     kamil #endif
    246  1.45.2.1  christos 		/* FALLTHROUGH */
    247       1.1  pgoyette 	case PT_TRACE_ME:
    248       1.1  pgoyette 	case PT_ATTACH:
    249       1.1  pgoyette 	case PT_WRITE_I:
    250       1.1  pgoyette 	case PT_WRITE_D:
    251       1.1  pgoyette 	case PT_READ_I:
    252       1.1  pgoyette 	case PT_READ_D:
    253       1.1  pgoyette 	case PT_IO:
    254      1.28  christos 	case_PT_GETREGS
    255      1.28  christos 	case_PT_SETREGS
    256      1.28  christos 	case_PT_GETFPREGS
    257      1.28  christos 	case_PT_SETFPREGS
    258      1.28  christos 	case_PT_GETDBREGS
    259       1.1  pgoyette 	case PT_SET_EVENT_MASK:
    260       1.1  pgoyette 	case PT_GET_EVENT_MASK:
    261       1.1  pgoyette 	case PT_GET_PROCESS_STATE:
    262       1.8     kamil 	case PT_SET_SIGINFO:
    263       1.8     kamil 	case PT_GET_SIGINFO:
    264       1.1  pgoyette #ifdef __HAVE_PTRACE_MACHDEP
    265       1.1  pgoyette 	PTRACE_MACHDEP_REQUEST_CASES
    266       1.1  pgoyette #endif
    267       1.1  pgoyette 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    268       1.1  pgoyette 		    ISSET(p->p_flag, PK_SUGID)) {
    269       1.1  pgoyette 			break;
    270       1.1  pgoyette 		}
    271       1.1  pgoyette 
    272       1.1  pgoyette 		result = KAUTH_RESULT_ALLOW;
    273       1.1  pgoyette 
    274       1.1  pgoyette 	break;
    275       1.1  pgoyette 
    276       1.1  pgoyette #ifdef PT_STEP
    277       1.1  pgoyette 	case PT_STEP:
    278      1.21     kamil 	case PT_SETSTEP:
    279      1.21     kamil 	case PT_CLEARSTEP:
    280       1.1  pgoyette #endif
    281       1.1  pgoyette 	case PT_CONTINUE:
    282       1.1  pgoyette 	case PT_KILL:
    283       1.1  pgoyette 	case PT_DETACH:
    284       1.1  pgoyette 	case PT_LWPINFO:
    285       1.1  pgoyette 	case PT_SYSCALL:
    286       1.1  pgoyette 	case PT_SYSCALLEMU:
    287       1.1  pgoyette 	case PT_DUMPCORE:
    288      1.15     kamil 	case PT_RESUME:
    289      1.15     kamil 	case PT_SUSPEND:
    290  1.45.2.2    martin 	case PT_STOP:
    291  1.45.2.2    martin 	case PT_LWPSTATUS:
    292  1.45.2.2    martin 	case PT_LWPNEXT:
    293       1.1  pgoyette 		result = KAUTH_RESULT_ALLOW;
    294       1.1  pgoyette 		break;
    295       1.1  pgoyette 
    296       1.1  pgoyette 	default:
    297       1.1  pgoyette 		break;
    298       1.1  pgoyette 	}
    299       1.1  pgoyette 
    300       1.1  pgoyette  out:
    301       1.1  pgoyette #if 0
    302       1.1  pgoyette 	mutex_enter(&ptrace_mtx);
    303       1.1  pgoyette 	if (--ptrace_cbref == 0)
    304       1.1  pgoyette 		cv_broadcast(&ptrace_cv);
    305       1.1  pgoyette 	mutex_exit(&ptrace_mtx);
    306       1.1  pgoyette #endif
    307       1.1  pgoyette 
    308       1.1  pgoyette 	return result;
    309       1.1  pgoyette }
    310       1.1  pgoyette 
    311       1.1  pgoyette int
    312       1.1  pgoyette ptrace_init(void)
    313       1.1  pgoyette {
    314       1.1  pgoyette 
    315       1.1  pgoyette #if 0
    316       1.1  pgoyette 	mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
    317       1.1  pgoyette 	cv_init(&ptrace_cv, "ptracecb");
    318       1.1  pgoyette 	ptrace_cbref = 0;
    319       1.1  pgoyette #endif
    320       1.1  pgoyette 	ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    321       1.1  pgoyette 	    ptrace_listener_cb, NULL);
    322       1.1  pgoyette 	return 0;
    323       1.1  pgoyette }
    324       1.1  pgoyette 
    325       1.1  pgoyette int
    326       1.1  pgoyette ptrace_fini(void)
    327       1.1  pgoyette {
    328       1.1  pgoyette 
    329       1.1  pgoyette 	kauth_unlisten_scope(ptrace_listener);
    330       1.1  pgoyette 
    331       1.1  pgoyette #if 0
    332       1.1  pgoyette 	/* Make sure no-one is executing our kauth listener */
    333       1.1  pgoyette 
    334       1.1  pgoyette 	mutex_enter(&ptrace_mtx);
    335       1.1  pgoyette 	while (ptrace_cbref != 0)
    336       1.1  pgoyette 		cv_wait(&ptrace_cv, &ptrace_mtx);
    337       1.1  pgoyette 	mutex_exit(&ptrace_mtx);
    338       1.1  pgoyette 	mutex_destroy(&ptrace_mtx);
    339       1.1  pgoyette 	cv_destroy(&ptrace_cv);
    340       1.1  pgoyette #endif
    341       1.1  pgoyette 
    342       1.1  pgoyette 	return 0;
    343       1.1  pgoyette }
    344       1.1  pgoyette 
    345      1.27  christos static struct proc *
    346      1.27  christos ptrace_find(struct lwp *l, int req, pid_t pid)
    347       1.1  pgoyette {
    348      1.27  christos 	struct proc *t;
    349       1.1  pgoyette 
    350       1.1  pgoyette 	/* "A foolish consistency..." XXX */
    351       1.1  pgoyette 	if (req == PT_TRACE_ME) {
    352      1.27  christos 		t = l->l_proc;
    353       1.1  pgoyette 		mutex_enter(t->p_lock);
    354      1.27  christos 		return t;
    355      1.27  christos 	}
    356       1.1  pgoyette 
    357      1.27  christos 	/* Find the process we're supposed to be operating on. */
    358      1.27  christos 	t = proc_find(pid);
    359      1.27  christos 	if (t == NULL)
    360      1.27  christos 		return NULL;
    361      1.27  christos 
    362      1.27  christos 	/* XXX-elad */
    363      1.27  christos 	mutex_enter(t->p_lock);
    364      1.27  christos 	int error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
    365      1.27  christos 	    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    366      1.27  christos 	if (error) {
    367      1.27  christos 		mutex_exit(t->p_lock);
    368      1.27  christos 		return NULL;
    369       1.1  pgoyette 	}
    370      1.27  christos 	return t;
    371      1.27  christos }
    372       1.1  pgoyette 
    373      1.27  christos static int
    374  1.45.2.2    martin ptrace_allowed(struct lwp *l, int req, struct proc *t, struct proc *p,
    375  1.45.2.2    martin     bool *locked)
    376      1.27  christos {
    377  1.45.2.2    martin 	*locked = false;
    378  1.45.2.2    martin 
    379       1.1  pgoyette 	/*
    380       1.1  pgoyette 	 * Grab a reference on the process to prevent it from execing or
    381       1.1  pgoyette 	 * exiting.
    382       1.1  pgoyette 	 */
    383      1.27  christos 	if (!rw_tryenter(&t->p_reflock, RW_READER))
    384       1.1  pgoyette 		return EBUSY;
    385       1.1  pgoyette 
    386  1.45.2.2    martin 	*locked = true;
    387  1.45.2.2    martin 
    388       1.1  pgoyette 	/* Make sure we can operate on it. */
    389       1.1  pgoyette 	switch (req) {
    390      1.28  christos 	case PT_TRACE_ME:
    391      1.38     kamil 		/*
    392      1.38     kamil 		 * You can't say to the parent of a process to start tracing if:
    393      1.38     kamil 		 *	(1) the parent is initproc,
    394      1.38     kamil 		 */
    395      1.38     kamil 		if (p->p_pptr == initproc)
    396      1.38     kamil 			return EPERM;
    397      1.38     kamil 
    398      1.38     kamil 		/*
    399      1.39     kamil 		 *	(2) the process is initproc, or
    400      1.39     kamil 		 */
    401      1.39     kamil 		if (p == initproc)
    402      1.39     kamil 			return EPERM;
    403      1.39     kamil 
    404      1.39     kamil 		/*
    405      1.39     kamil 		 *	(3) the child is already traced.
    406      1.38     kamil 		 */
    407      1.38     kamil 		if (ISSET(p->p_slflag, PSL_TRACED))
    408      1.38     kamil 			return EBUSY;
    409      1.38     kamil 
    410      1.27  christos 		return 0;
    411       1.1  pgoyette 
    412      1.28  christos 	case PT_ATTACH:
    413       1.1  pgoyette 		/*
    414       1.1  pgoyette 		 * You can't attach to a process if:
    415       1.1  pgoyette 		 *	(1) it's the process that's doing the attaching,
    416       1.1  pgoyette 		 */
    417      1.44     kamil 		if (t == p)
    418      1.27  christos 			return EINVAL;
    419       1.1  pgoyette 
    420       1.1  pgoyette 		/*
    421      1.39     kamil 		 *	(2) it's a system process,
    422       1.1  pgoyette 		 */
    423      1.27  christos 		if (t->p_flag & PK_SYSTEM)
    424      1.27  christos 			return EPERM;
    425       1.1  pgoyette 
    426       1.1  pgoyette 		/*
    427      1.39     kamil 		 *	(3) the tracer is initproc,
    428      1.39     kamil 		 */
    429      1.39     kamil 		if (p == initproc)
    430      1.39     kamil 			return EPERM;
    431      1.39     kamil 
    432      1.39     kamil 		/*
    433      1.43     kamil 		 *	(4) it's already being traced,
    434       1.1  pgoyette 		 */
    435      1.27  christos 		if (ISSET(t->p_slflag, PSL_TRACED))
    436      1.27  christos 			return EBUSY;
    437       1.1  pgoyette 
    438       1.1  pgoyette 		/*
    439      1.43     kamil 		 *	(5) it's a vfork(2)ed parent of the current process, or
    440      1.43     kamil 		 */
    441      1.43     kamil 		if (ISSET(p->p_lflag, PL_PPWAIT) && p->p_pptr == t)
    442      1.43     kamil 			return EPERM;
    443      1.43     kamil 
    444      1.43     kamil 		/*
    445      1.43     kamil 		 * 	(6) the tracer is chrooted, and its root directory is
    446       1.1  pgoyette 		 * 	    not at or above the root directory of the tracee
    447       1.1  pgoyette 		 */
    448       1.1  pgoyette 		mutex_exit(t->p_lock);	/* XXXSMP */
    449      1.27  christos 		int tmp = proc_isunder(t, l);
    450       1.1  pgoyette 		mutex_enter(t->p_lock);	/* XXXSMP */
    451      1.27  christos 		if (!tmp)
    452      1.27  christos 			return EPERM;
    453      1.27  christos 		return 0;
    454       1.1  pgoyette 
    455      1.28  christos 	case PT_READ_I:
    456      1.28  christos 	case PT_READ_D:
    457      1.28  christos 	case PT_WRITE_I:
    458      1.28  christos 	case PT_WRITE_D:
    459      1.28  christos 	case PT_IO:
    460      1.28  christos 	case PT_SET_SIGINFO:
    461      1.28  christos 	case PT_GET_SIGINFO:
    462      1.28  christos 	case_PT_GETREGS
    463      1.28  christos 	case_PT_SETREGS
    464      1.28  christos 	case_PT_GETFPREGS
    465      1.28  christos 	case_PT_SETFPREGS
    466      1.28  christos 	case_PT_GETDBREGS
    467      1.28  christos 	case_PT_SETDBREGS
    468       1.1  pgoyette #ifdef __HAVE_PTRACE_MACHDEP
    469       1.1  pgoyette 	PTRACE_MACHDEP_REQUEST_CASES
    470       1.1  pgoyette #endif
    471       1.1  pgoyette 		/*
    472       1.1  pgoyette 		 * You can't read/write the memory or registers of a process
    473       1.1  pgoyette 		 * if the tracer is chrooted, and its root directory is not at
    474       1.1  pgoyette 		 * or above the root directory of the tracee.
    475       1.1  pgoyette 		 */
    476       1.1  pgoyette 		mutex_exit(t->p_lock);	/* XXXSMP */
    477       1.1  pgoyette 		tmp = proc_isunder(t, l);
    478       1.1  pgoyette 		mutex_enter(t->p_lock);	/* XXXSMP */
    479      1.27  christos 		if (!tmp)
    480      1.27  christos 			return EPERM;
    481       1.1  pgoyette 		/*FALLTHROUGH*/
    482       1.1  pgoyette 
    483      1.28  christos 	case PT_CONTINUE:
    484      1.28  christos 	case PT_KILL:
    485      1.28  christos 	case PT_DETACH:
    486      1.28  christos 	case PT_LWPINFO:
    487      1.28  christos 	case PT_SYSCALL:
    488      1.28  christos 	case PT_SYSCALLEMU:
    489      1.28  christos 	case PT_DUMPCORE:
    490       1.1  pgoyette #ifdef PT_STEP
    491      1.28  christos 	case PT_STEP:
    492      1.28  christos 	case PT_SETSTEP:
    493      1.28  christos 	case PT_CLEARSTEP:
    494      1.28  christos #endif
    495      1.28  christos 	case PT_SET_EVENT_MASK:
    496      1.28  christos 	case PT_GET_EVENT_MASK:
    497      1.28  christos 	case PT_GET_PROCESS_STATE:
    498      1.28  christos 	case PT_RESUME:
    499      1.28  christos 	case PT_SUSPEND:
    500  1.45.2.2    martin 	case PT_STOP:
    501  1.45.2.2    martin 	case PT_LWPSTATUS:
    502  1.45.2.2    martin 	case PT_LWPNEXT:
    503       1.1  pgoyette 		/*
    504       1.1  pgoyette 		 * You can't do what you want to the process if:
    505       1.1  pgoyette 		 *	(1) It's not being traced at all,
    506       1.1  pgoyette 		 */
    507      1.27  christos 		if (!ISSET(t->p_slflag, PSL_TRACED))
    508      1.27  christos 			return EPERM;
    509       1.1  pgoyette 
    510       1.1  pgoyette 		/*
    511      1.23     kamil 		 *	(2) it's not being traced by _you_, or
    512       1.1  pgoyette 		 */
    513       1.1  pgoyette 		if (t->p_pptr != p) {
    514       1.1  pgoyette 			DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
    515       1.1  pgoyette 			    p->p_pid));
    516      1.27  christos 			return EBUSY;
    517       1.1  pgoyette 		}
    518       1.1  pgoyette 
    519       1.1  pgoyette 		/*
    520      1.23     kamil 		 *	(3) it's not currently stopped.
    521  1.45.2.2    martin 		 *
    522  1.45.2.2    martin 		 *	As an exception allow PT_KILL and PT_STOP here.
    523       1.1  pgoyette 		 */
    524  1.45.2.2    martin 		if (req != PT_KILL && req != PT_STOP &&
    525  1.45.2.2    martin 		    (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */)) {
    526       1.1  pgoyette 			DPRINTF(("stat %d flag %d\n", t->p_stat,
    527       1.1  pgoyette 			    !t->p_waited));
    528      1.27  christos 			return EBUSY;
    529       1.1  pgoyette 		}
    530      1.27  christos 		return 0;
    531       1.1  pgoyette 
    532       1.1  pgoyette 	default:			/* It was not a legal request. */
    533      1.27  christos 		return EINVAL;
    534      1.27  christos 	}
    535      1.27  christos }
    536      1.27  christos 
    537      1.27  christos static int
    538      1.27  christos ptrace_needs_hold(int req)
    539      1.27  christos {
    540      1.27  christos 	switch (req) {
    541      1.27  christos #ifdef PT_STEP
    542      1.27  christos 	case PT_STEP:
    543      1.27  christos #endif
    544      1.27  christos 	case PT_CONTINUE:
    545      1.27  christos 	case PT_DETACH:
    546      1.27  christos 	case PT_KILL:
    547      1.27  christos 	case PT_SYSCALL:
    548      1.27  christos 	case PT_SYSCALLEMU:
    549      1.27  christos 	case PT_ATTACH:
    550      1.27  christos 	case PT_TRACE_ME:
    551      1.27  christos 	case PT_GET_SIGINFO:
    552      1.27  christos 	case PT_SET_SIGINFO:
    553  1.45.2.2    martin 	case PT_STOP:
    554      1.27  christos 		return 1;
    555      1.27  christos 	default:
    556      1.27  christos 		return 0;
    557      1.27  christos 	}
    558      1.27  christos }
    559      1.27  christos 
    560      1.27  christos static int
    561      1.27  christos ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid)
    562      1.27  christos {
    563      1.27  christos 	if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1)
    564      1.27  christos 		return 0;
    565      1.27  christos 
    566      1.27  christos 	mutex_enter(t->p_lock);
    567  1.45.2.2    martin 	lwp_delref2(*lt);
    568  1.45.2.2    martin 
    569      1.27  christos 	*lt = lwp_find(t, lid);
    570      1.27  christos 	if (*lt == NULL) {
    571      1.27  christos 		mutex_exit(t->p_lock);
    572      1.27  christos 		return ESRCH;
    573      1.27  christos 	}
    574      1.27  christos 
    575      1.27  christos 	if ((*lt)->l_flag & LW_SYSTEM) {
    576  1.45.2.2    martin 		mutex_exit(t->p_lock);
    577      1.27  christos 		*lt = NULL;
    578      1.27  christos 		return EINVAL;
    579      1.27  christos 	}
    580      1.27  christos 
    581      1.27  christos 	lwp_addref(*lt);
    582      1.27  christos 	mutex_exit(t->p_lock);
    583      1.27  christos 
    584      1.27  christos 	return 0;
    585      1.27  christos }
    586      1.27  christos 
    587      1.27  christos static int
    588      1.29  christos ptrace_get_siginfo(struct proc *t, struct ptrace_methods *ptm, void *addr,
    589      1.29  christos     size_t data)
    590      1.27  christos {
    591      1.27  christos 	struct ptrace_siginfo psi;
    592      1.27  christos 
    593  1.45.2.1  christos 	memset(&psi, 0, sizeof(psi));
    594      1.27  christos 	psi.psi_siginfo._info = t->p_sigctx.ps_info;
    595      1.27  christos 	psi.psi_lwpid = t->p_sigctx.ps_lwp;
    596      1.45  christos 	DPRINTF(("%s: lwp=%d signal=%d\n", __func__, psi.psi_lwpid,
    597      1.45  christos 	    psi.psi_siginfo.si_signo));
    598      1.27  christos 
    599      1.29  christos 	return ptm->ptm_copyout_siginfo(&psi, addr, data);
    600      1.27  christos }
    601      1.27  christos 
    602      1.27  christos static int
    603      1.29  christos ptrace_set_siginfo(struct proc *t, struct lwp **lt, struct ptrace_methods *ptm,
    604      1.29  christos     void *addr, size_t data)
    605      1.27  christos {
    606      1.27  christos 	struct ptrace_siginfo psi;
    607      1.27  christos 
    608      1.29  christos 	int error = ptm->ptm_copyin_siginfo(&psi, addr, data);
    609      1.27  christos 	if (error)
    610      1.27  christos 		return error;
    611      1.27  christos 
    612      1.27  christos 	/* Check that the data is a valid signal number or zero. */
    613      1.27  christos 	if (psi.psi_siginfo.si_signo < 0 || psi.psi_siginfo.si_signo >= NSIG)
    614      1.27  christos 		return EINVAL;
    615      1.27  christos 
    616      1.27  christos 	t->p_sigctx.ps_faked = true;
    617      1.27  christos 	t->p_sigctx.ps_info = psi.psi_siginfo._info;
    618      1.27  christos 	t->p_sigctx.ps_lwp = psi.psi_lwpid;
    619      1.45  christos 	DPRINTF(("%s: lwp=%d signal=%d\n", __func__, psi.psi_lwpid,
    620      1.45  christos 	    psi.psi_siginfo.si_signo));
    621      1.27  christos 	return 0;
    622      1.27  christos }
    623      1.27  christos 
    624      1.27  christos static int
    625      1.27  christos ptrace_get_event_mask(struct proc *t, void *addr, size_t data)
    626      1.27  christos {
    627      1.27  christos 	struct ptrace_event pe;
    628      1.27  christos 
    629      1.27  christos 	if (data != sizeof(pe)) {
    630      1.27  christos 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe)));
    631      1.27  christos 		return EINVAL;
    632      1.27  christos 	}
    633      1.27  christos 	memset(&pe, 0, sizeof(pe));
    634      1.27  christos 	pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
    635      1.27  christos 	    PTRACE_FORK : 0;
    636      1.27  christos 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
    637      1.27  christos 	    PTRACE_VFORK : 0;
    638      1.27  christos 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
    639      1.27  christos 	    PTRACE_VFORK_DONE : 0;
    640      1.27  christos 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
    641      1.27  christos 	    PTRACE_LWP_CREATE : 0;
    642      1.27  christos 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
    643      1.27  christos 	    PTRACE_LWP_EXIT : 0;
    644  1.45.2.2    martin 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEPOSIX_SPAWN) ?
    645  1.45.2.2    martin 	    PTRACE_POSIX_SPAWN : 0;
    646      1.45  christos 	DPRINTF(("%s: lwp=%d event=%#x\n", __func__,
    647      1.45  christos 	    t->p_sigctx.ps_lwp, pe.pe_set_event));
    648      1.27  christos 	return copyout(&pe, addr, sizeof(pe));
    649      1.27  christos }
    650      1.27  christos 
    651      1.27  christos static int
    652      1.27  christos ptrace_set_event_mask(struct proc *t, void *addr, size_t data)
    653      1.27  christos {
    654      1.27  christos 	struct ptrace_event pe;
    655      1.27  christos 	int error;
    656      1.27  christos 
    657      1.27  christos 	if (data != sizeof(pe)) {
    658      1.27  christos 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe)));
    659      1.27  christos 		return EINVAL;
    660      1.27  christos 	}
    661      1.27  christos 	if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
    662      1.27  christos 		return error;
    663      1.27  christos 
    664      1.45  christos 	DPRINTF(("%s: lwp=%d event=%#x\n", __func__,
    665      1.45  christos 	    t->p_sigctx.ps_lwp, pe.pe_set_event));
    666      1.27  christos 	if (pe.pe_set_event & PTRACE_FORK)
    667      1.27  christos 		SET(t->p_slflag, PSL_TRACEFORK);
    668      1.27  christos 	else
    669      1.27  christos 		CLR(t->p_slflag, PSL_TRACEFORK);
    670      1.40     kamil 
    671      1.27  christos 	if (pe.pe_set_event & PTRACE_VFORK)
    672      1.27  christos 		SET(t->p_slflag, PSL_TRACEVFORK);
    673      1.27  christos 	else
    674      1.27  christos 		CLR(t->p_slflag, PSL_TRACEVFORK);
    675      1.40     kamil 
    676      1.27  christos 	if (pe.pe_set_event & PTRACE_VFORK_DONE)
    677      1.27  christos 		SET(t->p_slflag, PSL_TRACEVFORK_DONE);
    678      1.27  christos 	else
    679      1.27  christos 		CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
    680      1.40     kamil 
    681      1.27  christos 	if (pe.pe_set_event & PTRACE_LWP_CREATE)
    682      1.27  christos 		SET(t->p_slflag, PSL_TRACELWP_CREATE);
    683      1.27  christos 	else
    684      1.27  christos 		CLR(t->p_slflag, PSL_TRACELWP_CREATE);
    685      1.40     kamil 
    686      1.27  christos 	if (pe.pe_set_event & PTRACE_LWP_EXIT)
    687      1.27  christos 		SET(t->p_slflag, PSL_TRACELWP_EXIT);
    688      1.27  christos 	else
    689      1.27  christos 		CLR(t->p_slflag, PSL_TRACELWP_EXIT);
    690  1.45.2.2    martin 
    691  1.45.2.2    martin 	if (pe.pe_set_event & PTRACE_POSIX_SPAWN)
    692  1.45.2.2    martin 		SET(t->p_slflag, PSL_TRACEPOSIX_SPAWN);
    693  1.45.2.2    martin 	else
    694  1.45.2.2    martin 		CLR(t->p_slflag, PSL_TRACEPOSIX_SPAWN);
    695  1.45.2.2    martin 
    696      1.27  christos 	return 0;
    697      1.27  christos }
    698      1.27  christos 
    699      1.27  christos static int
    700      1.27  christos ptrace_get_process_state(struct proc *t, void *addr, size_t data)
    701      1.27  christos {
    702  1.45.2.2    martin 	struct _ksiginfo *si;
    703      1.27  christos 	struct ptrace_state ps;
    704      1.27  christos 
    705      1.27  christos 	if (data != sizeof(ps)) {
    706      1.27  christos 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(ps)));
    707      1.27  christos 		return EINVAL;
    708      1.27  christos 	}
    709      1.27  christos 
    710  1.45.2.2    martin 	if (t->p_sigctx.ps_info._signo != SIGTRAP ||
    711  1.45.2.2    martin 	    (t->p_sigctx.ps_info._code != TRAP_CHLD &&
    712  1.45.2.2    martin 	        t->p_sigctx.ps_info._code != TRAP_LWP)) {
    713  1.45.2.2    martin 		memset(&ps, 0, sizeof(ps));
    714  1.45.2.2    martin 	} else {
    715  1.45.2.2    martin 		si = &t->p_sigctx.ps_info;
    716  1.45.2.2    martin 
    717  1.45.2.2    martin 		KASSERT(si->_reason._ptrace_state._pe_report_event > 0);
    718  1.45.2.2    martin 		KASSERT(si->_reason._ptrace_state._option._pe_other_pid > 0);
    719  1.45.2.2    martin 
    720  1.45.2.2    martin 		ps.pe_report_event = si->_reason._ptrace_state._pe_report_event;
    721  1.45.2.2    martin 
    722  1.45.2.2    martin 		CTASSERT(sizeof(ps.pe_other_pid) == sizeof(ps.pe_lwp));
    723  1.45.2.2    martin 		ps.pe_other_pid =
    724  1.45.2.2    martin 			si->_reason._ptrace_state._option._pe_other_pid;
    725      1.27  christos 	}
    726  1.45.2.2    martin 
    727      1.45  christos 	DPRINTF(("%s: lwp=%d event=%#x pid=%d lwp=%d\n", __func__,
    728      1.45  christos 	    t->p_sigctx.ps_lwp, ps.pe_report_event,
    729      1.45  christos 	    ps.pe_other_pid, ps.pe_lwp));
    730      1.27  christos 	return copyout(&ps, addr, sizeof(ps));
    731      1.27  christos }
    732      1.27  christos 
    733      1.27  christos static int
    734      1.27  christos ptrace_lwpinfo(struct proc *t, struct lwp **lt, void *addr, size_t data)
    735      1.27  christos {
    736      1.27  christos 	struct ptrace_lwpinfo pl;
    737      1.27  christos 
    738      1.27  christos 	if (data != sizeof(pl)) {
    739      1.27  christos 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pl)));
    740      1.27  christos 		return EINVAL;
    741      1.27  christos 	}
    742      1.27  christos 	int error = copyin(addr, &pl, sizeof(pl));
    743      1.27  christos 	if (error)
    744      1.27  christos 		return error;
    745      1.27  christos 
    746      1.27  christos 	lwpid_t tmp = pl.pl_lwpid;
    747      1.27  christos 	lwp_delref(*lt);
    748      1.27  christos 	mutex_enter(t->p_lock);
    749      1.27  christos 	if (tmp == 0)
    750      1.27  christos 		*lt = lwp_find_first(t);
    751      1.27  christos 	else {
    752      1.27  christos 		*lt = lwp_find(t, tmp);
    753      1.27  christos 		if (*lt == NULL) {
    754      1.27  christos 			mutex_exit(t->p_lock);
    755      1.27  christos 			return ESRCH;
    756      1.27  christos 		}
    757      1.27  christos 		*lt = LIST_NEXT(*lt, l_sibling);
    758       1.1  pgoyette 	}
    759       1.1  pgoyette 
    760  1.45.2.2    martin 	while (*lt != NULL && (!lwp_alive(*lt) ||
    761  1.45.2.2    martin 	       ((*lt)->l_flag & LW_SYSTEM) != 0))
    762      1.27  christos 		*lt = LIST_NEXT(*lt, l_sibling);
    763      1.27  christos 
    764      1.27  christos 	pl.pl_lwpid = 0;
    765      1.27  christos 	pl.pl_event = 0;
    766      1.27  christos 	if (*lt) {
    767      1.27  christos 		lwp_addref(*lt);
    768      1.27  christos 		pl.pl_lwpid = (*lt)->l_lid;
    769      1.27  christos 
    770      1.27  christos 		if ((*lt)->l_flag & LW_WSUSPEND)
    771      1.27  christos 			pl.pl_event = PL_EVENT_SUSPENDED;
    772      1.27  christos 		/*
    773      1.27  christos 		 * If we match the lwp, or it was sent to every lwp,
    774      1.27  christos 		 * we set PL_EVENT_SIGNAL.
    775      1.27  christos 		 * XXX: ps_lwp == 0 means everyone and noone, so
    776      1.27  christos 		 * check ps_signo too.
    777      1.27  christos 		 */
    778      1.27  christos 		else if ((*lt)->l_lid == t->p_sigctx.ps_lwp
    779      1.27  christos 			 || (t->p_sigctx.ps_lwp == 0 &&
    780      1.45  christos 			     t->p_sigctx.ps_info._signo)) {
    781      1.45  christos 			DPRINTF(("%s: lwp=%d siglwp=%d signo %d\n", __func__,
    782      1.45  christos 			    pl.pl_lwpid, t->p_sigctx.ps_lwp,
    783      1.45  christos 			    t->p_sigctx.ps_info._signo));
    784      1.27  christos 			pl.pl_event = PL_EVENT_SIGNAL;
    785      1.45  christos 		}
    786       1.1  pgoyette 	}
    787      1.27  christos 	mutex_exit(t->p_lock);
    788      1.45  christos 	DPRINTF(("%s: lwp=%d event=%#x\n", __func__,
    789      1.45  christos 	    pl.pl_lwpid, pl.pl_event));
    790      1.27  christos 
    791      1.27  christos 	return copyout(&pl, addr, sizeof(pl));
    792      1.27  christos }
    793      1.27  christos 
    794      1.27  christos static int
    795  1.45.2.2    martin ptrace_lwpstatus(struct proc *t, struct ptrace_methods *ptm, struct lwp **lt,
    796  1.45.2.2    martin     void *addr, size_t data, bool next)
    797  1.45.2.2    martin {
    798  1.45.2.2    martin 	struct ptrace_lwpstatus pls;
    799  1.45.2.2    martin 	struct lwp *l;
    800  1.45.2.2    martin 	int error;
    801  1.45.2.2    martin 
    802  1.45.2.2    martin 	if (data > sizeof(pls) || data < sizeof(lwpid_t)) {
    803  1.45.2.2    martin 		DPRINTF(("%s: invalid data: %zu < %zu < %zu\n",
    804  1.45.2.2    martin 		        __func__, sizeof(lwpid_t), data, sizeof(pls)));
    805  1.45.2.2    martin 		return EINVAL;
    806  1.45.2.2    martin 	}
    807  1.45.2.2    martin 	error = copyin(addr, &pls.pl_lwpid, sizeof(lwpid_t));
    808  1.45.2.2    martin 	if (error)
    809  1.45.2.2    martin 		return error;
    810  1.45.2.2    martin 
    811  1.45.2.2    martin 	if (next) {
    812  1.45.2.2    martin 		lwp_delref(*lt);
    813  1.45.2.2    martin 		lwpid_t tmp = pls.pl_lwpid;
    814  1.45.2.2    martin 		mutex_enter(t->p_lock);
    815  1.45.2.2    martin 		if (tmp == 0)
    816  1.45.2.2    martin 			*lt = lwp_find_first(t);
    817  1.45.2.2    martin 		else {
    818  1.45.2.2    martin 			*lt = lwp_find(t, tmp);
    819  1.45.2.2    martin 			if (*lt == NULL) {
    820  1.45.2.2    martin 				mutex_exit(t->p_lock);
    821  1.45.2.2    martin 				return ESRCH;
    822  1.45.2.2    martin 			}
    823  1.45.2.2    martin 			*lt = LIST_NEXT(*lt, l_sibling);
    824  1.45.2.2    martin 		}
    825  1.45.2.2    martin 
    826  1.45.2.2    martin 		while (*lt != NULL && (!lwp_alive(*lt) ||
    827  1.45.2.2    martin 		       ((*lt)->l_flag & LW_SYSTEM) != 0))
    828  1.45.2.2    martin 			*lt = LIST_NEXT(*lt, l_sibling);
    829  1.45.2.2    martin 
    830  1.45.2.2    martin 		if (*lt == NULL) {
    831  1.45.2.2    martin 			memset(&pls, 0, sizeof(pls));
    832  1.45.2.2    martin 			mutex_exit(t->p_lock);
    833  1.45.2.2    martin 			goto out;
    834  1.45.2.2    martin 		}
    835  1.45.2.2    martin 		lwp_addref(*lt);
    836  1.45.2.2    martin 		mutex_exit(t->p_lock);
    837  1.45.2.2    martin 
    838  1.45.2.2    martin 		pls.pl_lwpid = (*lt)->l_lid;
    839  1.45.2.2    martin 	} else {
    840  1.45.2.2    martin 		if ((error = ptrace_update_lwp(t, lt, pls.pl_lwpid)) != 0)
    841  1.45.2.2    martin 			return error;
    842  1.45.2.2    martin 	}
    843  1.45.2.2    martin 
    844  1.45.2.2    martin 	l = *lt;
    845  1.45.2.2    martin 
    846  1.45.2.2    martin 	ptrace_read_lwpstatus(l, &pls);
    847  1.45.2.2    martin 
    848  1.45.2.2    martin out:
    849  1.45.2.2    martin 	DPRINTF(("%s: lwp=%d sigpend=%02x%02x%02x%02x sigmask=%02x%02x%02x%02x "
    850  1.45.2.2    martin 	   "name='%s' private=%p\n", __func__, pls.pl_lwpid,
    851  1.45.2.2    martin 	    pls.pl_sigpend.__bits[0], pls.pl_sigpend.__bits[1],
    852  1.45.2.2    martin 	    pls.pl_sigpend.__bits[2], pls.pl_sigpend.__bits[3],
    853  1.45.2.2    martin 	    pls.pl_sigmask.__bits[0], pls.pl_sigmask.__bits[1],
    854  1.45.2.2    martin 	    pls.pl_sigmask.__bits[2], pls.pl_sigmask.__bits[3],
    855  1.45.2.2    martin 	    pls.pl_name, pls.pl_private));
    856  1.45.2.2    martin 
    857  1.45.2.2    martin 	return ptm->ptm_copyout_lwpstatus(&pls, addr, data);
    858  1.45.2.2    martin }
    859  1.45.2.2    martin 
    860  1.45.2.2    martin static int
    861      1.27  christos ptrace_startstop(struct proc *t, struct lwp **lt, int rq, void *addr,
    862      1.27  christos     size_t data)
    863      1.27  christos {
    864      1.27  christos 	int error;
    865      1.27  christos 
    866      1.27  christos 	if ((error = ptrace_update_lwp(t, lt, data)) != 0)
    867      1.27  christos 		return error;
    868      1.27  christos 
    869      1.45  christos 	DPRINTF(("%s: lwp=%d request=%d\n", __func__, (*lt)->l_lid, rq));
    870      1.27  christos 	lwp_lock(*lt);
    871      1.27  christos 	if (rq == PT_SUSPEND)
    872  1.45.2.2    martin 		(*lt)->l_flag |= LW_DBGSUSPEND;
    873  1.45.2.2    martin 	else {
    874  1.45.2.2    martin 		(*lt)->l_flag &= ~LW_DBGSUSPEND;
    875  1.45.2.2    martin 		if ((*lt)->l_flag != LSSUSPENDED)
    876  1.45.2.2    martin 			(*lt)->l_stat = LSSTOP;
    877  1.45.2.2    martin 	}
    878      1.27  christos 	lwp_unlock(*lt);
    879      1.27  christos 	return 0;
    880      1.27  christos }
    881      1.27  christos 
    882      1.28  christos #ifdef PT_REGISTERS
    883      1.27  christos static int
    884      1.27  christos ptrace_uio_dir(int req)
    885      1.27  christos {
    886      1.27  christos 	switch (req) {
    887      1.28  christos 	case_PT_GETREGS
    888      1.28  christos 	case_PT_GETFPREGS
    889      1.28  christos 	case_PT_GETDBREGS
    890      1.27  christos 		return UIO_READ;
    891      1.28  christos 	case_PT_SETREGS
    892      1.28  christos 	case_PT_SETFPREGS
    893      1.28  christos 	case_PT_SETDBREGS
    894      1.27  christos 		return UIO_WRITE;
    895      1.27  christos 	default:
    896      1.27  christos 		return -1;
    897       1.1  pgoyette 	}
    898      1.27  christos }
    899       1.1  pgoyette 
    900      1.27  christos static int
    901      1.27  christos ptrace_regs(struct lwp *l, struct lwp **lt, int rq, struct ptrace_methods *ptm,
    902      1.27  christos     void *addr, size_t data)
    903      1.27  christos {
    904      1.27  christos 	int error;
    905  1.45.2.2    martin 	struct proc *p, *t;
    906      1.27  christos 	struct vmspace *vm;
    907      1.27  christos 
    908  1.45.2.2    martin 	p = l->l_proc;		/* tracer */
    909  1.45.2.2    martin 	t = (*lt)->l_proc;	/* traced */
    910  1.45.2.2    martin 
    911      1.27  christos 	if ((error = ptrace_update_lwp(t, lt, data)) != 0)
    912      1.27  christos 		return error;
    913      1.27  christos 
    914      1.27  christos 	int dir = ptrace_uio_dir(rq);
    915      1.27  christos 	size_t size;
    916      1.27  christos 	int (*func)(struct lwp *, struct lwp *, struct uio *);
    917      1.27  christos 
    918      1.45  christos 	DPRINTF(("%s: lwp=%d request=%d\n", __func__, l->l_lid, rq));
    919      1.45  christos 
    920      1.27  christos 	switch (rq) {
    921      1.27  christos #if defined(PT_SETREGS) || defined(PT_GETREGS)
    922      1.28  christos 	case_PT_GETREGS
    923      1.28  christos 	case_PT_SETREGS
    924      1.27  christos 		if (!process_validregs(*lt))
    925      1.27  christos 			return EINVAL;
    926  1.45.2.2    martin 		size = PROC_REGSZ(p);
    927      1.27  christos 		func = ptm->ptm_doregs;
    928      1.27  christos 		break;
    929      1.27  christos #endif
    930      1.27  christos #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
    931      1.28  christos 	case_PT_GETFPREGS
    932      1.28  christos 	case_PT_SETFPREGS
    933      1.27  christos 		if (!process_validfpregs(*lt))
    934      1.27  christos 			return EINVAL;
    935  1.45.2.2    martin 		size = PROC_FPREGSZ(p);
    936      1.27  christos 		func = ptm->ptm_dofpregs;
    937      1.27  christos 		break;
    938      1.27  christos #endif
    939      1.27  christos #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
    940      1.28  christos 	case_PT_GETDBREGS
    941      1.28  christos 	case_PT_SETDBREGS
    942      1.27  christos 		if (!process_validdbregs(*lt))
    943      1.27  christos 			return EINVAL;
    944  1.45.2.2    martin 		size = PROC_DBREGSZ(p);
    945      1.27  christos 		func = ptm->ptm_dodbregs;
    946      1.27  christos 		break;
    947      1.27  christos #endif
    948      1.27  christos 	default:
    949      1.27  christos 		return EINVAL;
    950      1.27  christos 	}
    951      1.27  christos 
    952      1.34  christos 	error = proc_vmspace_getref(l->l_proc, &vm);
    953      1.27  christos 	if (error)
    954      1.27  christos 		return error;
    955      1.27  christos 
    956      1.27  christos 	struct uio uio;
    957      1.27  christos 	struct iovec iov;
    958      1.27  christos 
    959      1.27  christos 	iov.iov_base = addr;
    960      1.27  christos 	iov.iov_len = size;
    961      1.27  christos 	uio.uio_iov = &iov;
    962      1.27  christos 	uio.uio_iovcnt = 1;
    963      1.27  christos 	uio.uio_offset = 0;
    964      1.27  christos 	uio.uio_resid = iov.iov_len;
    965      1.27  christos 	uio.uio_rw = dir;
    966      1.27  christos 	uio.uio_vmspace = vm;
    967      1.27  christos 
    968      1.27  christos 	error = (*func)(l, *lt, &uio);
    969      1.27  christos 	uvmspace_free(vm);
    970      1.27  christos 	return error;
    971      1.27  christos }
    972      1.28  christos #endif
    973      1.28  christos 
    974      1.28  christos static int
    975  1.45.2.2    martin ptrace_sendsig(struct lwp *l, int req, struct proc *t, struct lwp *lt, int signo, int resume_all)
    976      1.28  christos {
    977      1.28  christos 	ksiginfo_t ksi;
    978      1.28  christos 
    979      1.28  christos 	/* Finally, deliver the requested signal (or none). */
    980      1.28  christos 	if (t->p_stat == SSTOP) {
    981      1.28  christos 		/*
    982      1.28  christos 		 * Unstop the process.  If it needs to take a
    983      1.28  christos 		 * signal, make all efforts to ensure that at
    984      1.28  christos 		 * an LWP runs to see it.
    985      1.28  christos 		 */
    986      1.28  christos 		t->p_xsig = signo;
    987      1.41     kamil 
    988      1.41     kamil 		/*
    989      1.42     kamil 		 * signo > 0 check prevents a potential panic, as
    990      1.41     kamil 		 * sigismember(&...,0) is invalid check and signo
    991      1.41     kamil 		 * can be equal to 0 as a special case of no-signal.
    992      1.41     kamil 		 */
    993      1.41     kamil 		if (signo > 0 && sigismember(&stopsigmask, signo)) {
    994      1.41     kamil 			t->p_waited = 0;
    995      1.41     kamil 			child_psignal(t, 0);
    996      1.41     kamil 		} else if (resume_all)
    997      1.28  christos 			proc_unstop(t);
    998      1.28  christos 		else
    999      1.28  christos 			lwp_unstop(lt);
   1000      1.28  christos 		return 0;
   1001      1.28  christos 	}
   1002      1.28  christos 
   1003  1.45.2.2    martin 	KASSERT(req == PT_KILL || req == PT_STOP || req == PT_ATTACH);
   1004  1.45.2.2    martin 
   1005  1.45.2.2    martin 	KSI_INIT(&ksi);
   1006  1.45.2.2    martin 	ksi.ksi_signo = signo;
   1007  1.45.2.2    martin 	ksi.ksi_code = SI_USER;
   1008  1.45.2.2    martin 	ksi.ksi_pid = l->l_proc->p_pid;
   1009  1.45.2.2    martin 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
   1010  1.45.2.2    martin 
   1011  1.45.2.2    martin 	t->p_sigctx.ps_faked = false;
   1012  1.45.2.2    martin 
   1013      1.45  christos 	DPRINTF(("%s: pid=%d.%d signal=%d resume_all=%d\n", __func__, t->p_pid,
   1014  1.45.2.2    martin 	    lt->l_lid, signo, resume_all));
   1015      1.28  christos 
   1016  1.45.2.2    martin 	return kpsignal2(t, &ksi);
   1017      1.28  christos }
   1018      1.28  christos 
   1019      1.28  christos static int
   1020      1.28  christos ptrace_dumpcore(struct lwp *lt, char *path, size_t len)
   1021      1.28  christos {
   1022      1.28  christos 	int error;
   1023      1.28  christos 	if (path != NULL) {
   1024      1.28  christos 
   1025      1.28  christos 		if (len >= MAXPATHLEN)
   1026      1.28  christos 			return EINVAL;
   1027      1.28  christos 
   1028      1.28  christos 		char *src = path;
   1029      1.28  christos 		path = kmem_alloc(len + 1, KM_SLEEP);
   1030      1.28  christos 		error = copyin(src, path, len);
   1031      1.28  christos 		if (error)
   1032      1.28  christos 			goto out;
   1033      1.28  christos 		path[len] = '\0';
   1034      1.28  christos 	}
   1035      1.45  christos 	DPRINTF(("%s: lwp=%d\n", __func__, lt->l_lid));
   1036  1.45.2.2    martin 	MODULE_HOOK_CALL(coredump_hook, (lt, path), 0, error);
   1037      1.28  christos out:
   1038      1.28  christos 	if (path)
   1039      1.28  christos 		kmem_free(path, len + 1);
   1040      1.28  christos 	return error;
   1041      1.28  christos }
   1042      1.28  christos 
   1043      1.28  christos static int
   1044      1.28  christos ptrace_doio(struct lwp *l, struct proc *t, struct lwp *lt,
   1045      1.31  christos     struct ptrace_io_desc *piod, void *addr, bool sysspace)
   1046      1.28  christos {
   1047      1.28  christos 	struct uio uio;
   1048      1.28  christos 	struct iovec iov;
   1049      1.28  christos 	int error, tmp;
   1050      1.28  christos 
   1051      1.28  christos 	error = 0;
   1052      1.28  christos 	iov.iov_base = piod->piod_addr;
   1053      1.28  christos 	iov.iov_len = piod->piod_len;
   1054      1.28  christos 	uio.uio_iov = &iov;
   1055      1.28  christos 	uio.uio_iovcnt = 1;
   1056      1.28  christos 	uio.uio_offset = (off_t)(unsigned long)piod->piod_offs;
   1057      1.28  christos 	uio.uio_resid = piod->piod_len;
   1058      1.28  christos 
   1059      1.45  christos 	DPRINTF(("%s: lwp=%d request=%d\n", __func__, l->l_lid, piod->piod_op));
   1060      1.45  christos 
   1061      1.28  christos 	switch (piod->piod_op) {
   1062      1.28  christos 	case PIOD_READ_D:
   1063      1.28  christos 	case PIOD_READ_I:
   1064      1.28  christos 		uio.uio_rw = UIO_READ;
   1065      1.28  christos 		break;
   1066      1.28  christos 	case PIOD_WRITE_D:
   1067      1.28  christos 	case PIOD_WRITE_I:
   1068      1.28  christos 		/*
   1069      1.28  christos 		 * Can't write to a RAS
   1070      1.28  christos 		 */
   1071      1.28  christos 		if (ras_lookup(t, addr) != (void *)-1) {
   1072      1.28  christos 			return EACCES;
   1073      1.28  christos 		}
   1074      1.28  christos 		uio.uio_rw = UIO_WRITE;
   1075      1.28  christos 		break;
   1076      1.28  christos 	case PIOD_READ_AUXV:
   1077      1.28  christos 		uio.uio_rw = UIO_READ;
   1078      1.28  christos 		tmp = t->p_execsw->es_arglen;
   1079      1.28  christos 		if (uio.uio_offset > tmp)
   1080      1.28  christos 			return EIO;
   1081      1.28  christos 		if (uio.uio_resid > tmp - uio.uio_offset)
   1082      1.28  christos 			uio.uio_resid = tmp - uio.uio_offset;
   1083      1.28  christos 		piod->piod_len = iov.iov_len = uio.uio_resid;
   1084      1.28  christos 		error = process_auxv_offset(t, &uio);
   1085      1.28  christos 		break;
   1086      1.28  christos 	default:
   1087      1.28  christos 		error = EINVAL;
   1088      1.28  christos 		break;
   1089      1.28  christos 	}
   1090      1.31  christos 
   1091      1.28  christos 	if (error)
   1092      1.28  christos 		return error;
   1093      1.28  christos 
   1094      1.31  christos 	if (sysspace) {
   1095      1.31  christos 		uio.uio_vmspace = vmspace_kernel();
   1096      1.31  christos 	} else {
   1097      1.31  christos 		error = proc_vmspace_getref(l->l_proc, &uio.uio_vmspace);
   1098      1.31  christos 		if (error)
   1099      1.31  christos 			return error;
   1100      1.31  christos 	}
   1101      1.28  christos 
   1102      1.28  christos 	error = process_domem(l, lt, &uio);
   1103      1.31  christos 	if (!sysspace)
   1104      1.31  christos 		uvmspace_free(uio.uio_vmspace);
   1105      1.31  christos 	if (error)
   1106      1.28  christos 		return error;
   1107      1.28  christos 	piod->piod_len -= uio.uio_resid;
   1108      1.28  christos 	return 0;
   1109      1.28  christos }
   1110      1.27  christos 
   1111      1.27  christos int
   1112      1.27  christos do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
   1113      1.27  christos     void *addr, int data, register_t *retval)
   1114      1.27  christos {
   1115      1.27  christos 	struct proc *p = l->l_proc;
   1116      1.27  christos 	struct lwp *lt = NULL;
   1117      1.27  christos 	struct lwp *lt2;
   1118      1.27  christos 	struct proc *t;				/* target process */
   1119      1.27  christos 	struct ptrace_io_desc piod;
   1120      1.27  christos 	int error, write, tmp, pheld;
   1121      1.27  christos 	int signo = 0;
   1122      1.27  christos 	int resume_all;
   1123  1.45.2.2    martin 	bool locked;
   1124      1.27  christos 	error = 0;
   1125      1.27  christos 
   1126      1.27  christos 	/*
   1127      1.27  christos 	 * If attaching or detaching, we need to get a write hold on the
   1128      1.27  christos 	 * proclist lock so that we can re-parent the target process.
   1129      1.27  christos 	 */
   1130      1.27  christos 	mutex_enter(proc_lock);
   1131      1.27  christos 
   1132      1.27  christos 	t = ptrace_find(l, req, pid);
   1133      1.27  christos 	if (t == NULL) {
   1134       1.1  pgoyette 		mutex_exit(proc_lock);
   1135      1.27  christos 		return ESRCH;
   1136      1.27  christos 	}
   1137      1.27  christos 
   1138      1.27  christos 	pheld = 1;
   1139  1.45.2.2    martin 	if ((error = ptrace_allowed(l, req, t, p, &locked)) != 0)
   1140      1.27  christos 		goto out;
   1141      1.27  christos 
   1142      1.27  christos 	if ((error = kauth_authorize_process(l->l_cred,
   1143      1.27  christos 	    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req), NULL, NULL)) != 0)
   1144      1.27  christos 		goto out;
   1145      1.27  christos 
   1146      1.27  christos 	if ((lt = lwp_find_first(t)) == NULL) {
   1147      1.27  christos 	    error = ESRCH;
   1148      1.27  christos 	    goto out;
   1149       1.1  pgoyette 	}
   1150       1.1  pgoyette 
   1151       1.1  pgoyette 	/* Do single-step fixup if needed. */
   1152       1.1  pgoyette 	FIX_SSTEP(t);
   1153       1.1  pgoyette 	KASSERT(lt != NULL);
   1154       1.1  pgoyette 	lwp_addref(lt);
   1155       1.1  pgoyette 
   1156       1.1  pgoyette 	/*
   1157       1.1  pgoyette 	 * Which locks do we need held? XXX Ugly.
   1158       1.1  pgoyette 	 */
   1159      1.27  christos 	if ((pheld = ptrace_needs_hold(req)) == 0) {
   1160      1.27  christos 		mutex_exit(t->p_lock);
   1161       1.1  pgoyette 		mutex_exit(proc_lock);
   1162       1.1  pgoyette 	}
   1163       1.1  pgoyette 
   1164       1.1  pgoyette 	/* Now do the operation. */
   1165       1.1  pgoyette 	write = 0;
   1166       1.1  pgoyette 	*retval = 0;
   1167       1.1  pgoyette 	tmp = 0;
   1168       1.1  pgoyette 	resume_all = 1;
   1169       1.1  pgoyette 
   1170       1.1  pgoyette 	switch (req) {
   1171      1.28  christos 	case PT_TRACE_ME:
   1172       1.1  pgoyette 		/* Just set the trace flag. */
   1173       1.1  pgoyette 		SET(t->p_slflag, PSL_TRACED);
   1174       1.1  pgoyette 		t->p_opptr = t->p_pptr;
   1175       1.1  pgoyette 		break;
   1176       1.1  pgoyette 
   1177      1.37     kamil 	/*
   1178      1.37     kamil 	 * The I and D separate address space has been inherited from PDP-11.
   1179      1.37     kamil 	 * The 16-bit UNIX started with a single address space per program,
   1180      1.37     kamil 	 * but was extended to two 16-bit (2 x 64kb) address spaces.
   1181      1.37     kamil 	 *
   1182      1.37     kamil 	 * We no longer maintain this feature in maintained architectures, but
   1183      1.37     kamil 	 * we keep the API for backward compatiblity. Currently the I and D
   1184      1.37     kamil 	 * operations are exactly the same and not distinguished in debuggers.
   1185      1.37     kamil 	 */
   1186      1.37     kamil 	case PT_WRITE_I:
   1187      1.28  christos 	case PT_WRITE_D:
   1188       1.1  pgoyette 		write = 1;
   1189       1.1  pgoyette 		tmp = data;
   1190       1.1  pgoyette 		/* FALLTHROUGH */
   1191      1.37     kamil 	case PT_READ_I:
   1192      1.28  christos 	case PT_READ_D:
   1193      1.31  christos 		piod.piod_addr = &tmp;
   1194      1.31  christos 		piod.piod_len = sizeof(tmp);
   1195      1.31  christos 		piod.piod_offs = addr;
   1196      1.31  christos 		piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
   1197      1.31  christos 		if ((error = ptrace_doio(l, t, lt, &piod, addr, true)) != 0)
   1198      1.31  christos 			break;
   1199  1.45.2.1  christos 		/*
   1200  1.45.2.1  christos 		 * For legacy reasons we treat here two results as success:
   1201  1.45.2.1  christos 		 *  - incomplete transfer  piod.piod_len < sizeof(tmp)
   1202  1.45.2.1  christos 		 *  - no transfer          piod.piod_len == 0
   1203  1.45.2.1  christos 		 *
   1204  1.45.2.1  christos 		 * This means that there is no way to determine whether
   1205  1.45.2.1  christos 		 * transfer operation was performed in PT_WRITE and PT_READ
   1206  1.45.2.1  christos 		 * calls.
   1207  1.45.2.1  christos 		 */
   1208       1.1  pgoyette 		if (!write)
   1209       1.1  pgoyette 			*retval = tmp;
   1210       1.1  pgoyette 		break;
   1211       1.1  pgoyette 
   1212      1.28  christos 	case PT_IO:
   1213      1.29  christos 		if ((error = ptm->ptm_copyin_piod(&piod, addr, data)) != 0)
   1214       1.1  pgoyette 			break;
   1215  1.45.2.1  christos 		if (piod.piod_len < 1) {
   1216  1.45.2.1  christos 			error = EINVAL;
   1217  1.45.2.1  christos 			break;
   1218  1.45.2.1  christos 		}
   1219      1.31  christos 		if ((error = ptrace_doio(l, t, lt, &piod, addr, false)) != 0)
   1220       1.1  pgoyette 			break;
   1221  1.45.2.1  christos 		/*
   1222  1.45.2.1  christos 		 * For legacy reasons we treat here two results as success:
   1223  1.45.2.1  christos 		 *  - incomplete transfer  piod.piod_len < sizeof(tmp)
   1224  1.45.2.1  christos 		 *  - no transfer          piod.piod_len == 0
   1225  1.45.2.1  christos 		 */
   1226      1.33  christos 		error = ptm->ptm_copyout_piod(&piod, addr, data);
   1227       1.1  pgoyette 		break;
   1228       1.1  pgoyette 
   1229      1.28  christos 	case PT_DUMPCORE:
   1230      1.28  christos 		error = ptrace_dumpcore(lt, addr, data);
   1231       1.1  pgoyette 		break;
   1232       1.1  pgoyette 
   1233       1.1  pgoyette #ifdef PT_STEP
   1234      1.28  christos 	case PT_STEP:
   1235       1.1  pgoyette 		/*
   1236       1.1  pgoyette 		 * From the 4.4BSD PRM:
   1237       1.1  pgoyette 		 * "Execution continues as in request PT_CONTINUE; however
   1238       1.1  pgoyette 		 * as soon as possible after execution of at least one
   1239       1.1  pgoyette 		 * instruction, execution stops again. [ ... ]"
   1240       1.1  pgoyette 		 */
   1241       1.1  pgoyette #endif
   1242      1.28  christos 	case PT_CONTINUE:
   1243      1.28  christos 	case PT_SYSCALL:
   1244      1.28  christos 	case PT_DETACH:
   1245       1.1  pgoyette 		if (req == PT_SYSCALL) {
   1246       1.1  pgoyette 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
   1247       1.1  pgoyette 				SET(t->p_slflag, PSL_SYSCALL);
   1248       1.1  pgoyette #ifdef __HAVE_SYSCALL_INTERN
   1249       1.1  pgoyette 				(*t->p_emul->e_syscall_intern)(t);
   1250       1.1  pgoyette #endif
   1251       1.1  pgoyette 			}
   1252       1.1  pgoyette 		} else {
   1253       1.1  pgoyette 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
   1254       1.1  pgoyette 				CLR(t->p_slflag, PSL_SYSCALL);
   1255       1.1  pgoyette #ifdef __HAVE_SYSCALL_INTERN
   1256       1.1  pgoyette 				(*t->p_emul->e_syscall_intern)(t);
   1257       1.1  pgoyette #endif
   1258       1.1  pgoyette 			}
   1259       1.1  pgoyette 		}
   1260       1.1  pgoyette 		t->p_trace_enabled = trace_is_enabled(t);
   1261       1.1  pgoyette 
   1262       1.1  pgoyette 		/*
   1263       1.1  pgoyette 		 * Pick up the LWPID, if supplied.  There are two cases:
   1264       1.1  pgoyette 		 * data < 0 : step or continue single thread, lwp = -data
   1265       1.1  pgoyette 		 * data > 0 in PT_STEP : step this thread, continue others
   1266       1.1  pgoyette 		 * For operations other than PT_STEP, data > 0 means
   1267       1.1  pgoyette 		 * data is the signo to deliver to the process.
   1268       1.1  pgoyette 		 */
   1269       1.1  pgoyette 		tmp = data;
   1270       1.1  pgoyette 		if (tmp >= 0) {
   1271       1.1  pgoyette #ifdef PT_STEP
   1272       1.1  pgoyette 			if (req == PT_STEP)
   1273       1.1  pgoyette 				signo = 0;
   1274       1.1  pgoyette 			else
   1275       1.1  pgoyette #endif
   1276       1.1  pgoyette 			{
   1277       1.1  pgoyette 				signo = tmp;
   1278       1.1  pgoyette 				tmp = 0;	/* don't search for LWP */
   1279       1.1  pgoyette 			}
   1280  1.45.2.2    martin 		} else if (tmp == INT_MIN) {
   1281  1.45.2.2    martin 			error = ESRCH;
   1282  1.45.2.2    martin 			break;
   1283  1.45.2.2    martin 		} else {
   1284       1.1  pgoyette 			tmp = -tmp;
   1285  1.45.2.2    martin 		}
   1286       1.1  pgoyette 
   1287       1.1  pgoyette 		if (tmp > 0) {
   1288       1.1  pgoyette 			if (req == PT_DETACH) {
   1289       1.1  pgoyette 				error = EINVAL;
   1290       1.1  pgoyette 				break;
   1291       1.1  pgoyette 			}
   1292       1.1  pgoyette 			lwp_delref2 (lt);
   1293       1.1  pgoyette 			lt = lwp_find(t, tmp);
   1294       1.1  pgoyette 			if (lt == NULL) {
   1295       1.1  pgoyette 				error = ESRCH;
   1296       1.1  pgoyette 				break;
   1297       1.1  pgoyette 			}
   1298       1.1  pgoyette 			lwp_addref(lt);
   1299       1.1  pgoyette 			resume_all = 0;
   1300       1.1  pgoyette 			signo = 0;
   1301       1.1  pgoyette 		}
   1302       1.1  pgoyette 
   1303       1.1  pgoyette 		/*
   1304       1.1  pgoyette 		 * From the 4.4BSD PRM:
   1305       1.1  pgoyette 		 * "The data argument is taken as a signal number and the
   1306       1.1  pgoyette 		 * child's execution continues at location addr as if it
   1307       1.1  pgoyette 		 * incurred that signal.  Normally the signal number will
   1308       1.1  pgoyette 		 * be either 0 to indicate that the signal that caused the
   1309       1.1  pgoyette 		 * stop should be ignored, or that value fetched out of
   1310       1.1  pgoyette 		 * the process's image indicating which signal caused
   1311       1.1  pgoyette 		 * the stop.  If addr is (int *)1 then execution continues
   1312       1.1  pgoyette 		 * from where it stopped."
   1313       1.1  pgoyette 		 */
   1314       1.1  pgoyette 
   1315       1.1  pgoyette 		/* Check that the data is a valid signal number or zero. */
   1316       1.1  pgoyette 		if (signo < 0 || signo >= NSIG) {
   1317       1.1  pgoyette 			error = EINVAL;
   1318       1.1  pgoyette 			break;
   1319       1.1  pgoyette 		}
   1320       1.1  pgoyette 
   1321      1.15     kamil 		/* Prevent process deadlock */
   1322      1.15     kamil 		if (resume_all) {
   1323      1.15     kamil #ifdef PT_STEP
   1324      1.15     kamil 			if (req == PT_STEP) {
   1325  1.45.2.2    martin 				if (lt->l_flag &
   1326  1.45.2.2    martin 				    (LW_WSUSPEND | LW_DBGSUSPEND)) {
   1327      1.15     kamil 					error = EDEADLK;
   1328      1.15     kamil 					break;
   1329      1.15     kamil 				}
   1330      1.15     kamil 			} else
   1331      1.15     kamil #endif
   1332      1.15     kamil 			{
   1333      1.15     kamil 				error = EDEADLK;
   1334      1.15     kamil 				LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
   1335  1.45.2.2    martin 					if ((lt2->l_flag &
   1336  1.45.2.2    martin 					    (LW_WSUSPEND | LW_DBGSUSPEND)) == 0
   1337  1.45.2.2    martin 					    ) {
   1338      1.15     kamil 						error = 0;
   1339      1.15     kamil 						break;
   1340      1.15     kamil 					}
   1341      1.15     kamil 				}
   1342      1.15     kamil 				if (error != 0)
   1343      1.15     kamil 					break;
   1344      1.15     kamil 			}
   1345      1.15     kamil 		} else {
   1346  1.45.2.2    martin 			if (lt->l_flag & (LW_WSUSPEND | LW_DBGSUSPEND)) {
   1347      1.15     kamil 				error = EDEADLK;
   1348      1.15     kamil 				break;
   1349      1.15     kamil 			}
   1350      1.15     kamil 		}
   1351      1.15     kamil 
   1352  1.45.2.1  christos 		/*
   1353  1.45.2.2    martin 		 * Reject setting program counter to 0x0 if VA0 is disabled.
   1354  1.45.2.1  christos 		 *
   1355  1.45.2.1  christos 		 * Not all kernels implement this feature to set Program
   1356  1.45.2.1  christos 		 * Counter in one go in PT_CONTINUE and similar operations.
   1357  1.45.2.1  christos 		 * This causes portability issues as passing address 0x0
   1358  1.45.2.1  christos 		 * on these kernels is no-operation, but can cause failure
   1359  1.45.2.1  christos 		 * in most cases on NetBSD.
   1360  1.45.2.1  christos 		 */
   1361  1.45.2.1  christos 		if (user_va0_disable && addr == 0) {
   1362  1.45.2.1  christos 			error = EINVAL;
   1363  1.45.2.1  christos 			break;
   1364  1.45.2.1  christos 		}
   1365  1.45.2.1  christos 
   1366       1.1  pgoyette 		/* If the address parameter is not (int *)1, set the pc. */
   1367       1.1  pgoyette 		if ((int *)addr != (int *)1) {
   1368       1.1  pgoyette 			error = process_set_pc(lt, addr);
   1369       1.1  pgoyette 			if (error != 0)
   1370       1.1  pgoyette 				break;
   1371       1.1  pgoyette 		}
   1372       1.1  pgoyette #ifdef PT_STEP
   1373       1.1  pgoyette 		/*
   1374       1.1  pgoyette 		 * Arrange for a single-step, if that's requested and possible.
   1375       1.1  pgoyette 		 * More precisely, set the single step status as requested for
   1376       1.1  pgoyette 		 * the requested thread, and clear it for other threads.
   1377       1.1  pgoyette 		 */
   1378       1.1  pgoyette 		LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
   1379      1.21     kamil 			if (ISSET(lt2->l_pflag, LP_SINGLESTEP)) {
   1380      1.21     kamil 				lwp_lock(lt2);
   1381      1.21     kamil 				process_sstep(lt2, 1);
   1382      1.21     kamil 				lwp_unlock(lt2);
   1383      1.21     kamil 			} else if (lt != lt2) {
   1384       1.1  pgoyette 				lwp_lock(lt2);
   1385       1.1  pgoyette 				process_sstep(lt2, 0);
   1386       1.1  pgoyette 				lwp_unlock(lt2);
   1387       1.1  pgoyette 			}
   1388       1.1  pgoyette 		}
   1389      1.21     kamil 		error = process_sstep(lt,
   1390      1.21     kamil 		    ISSET(lt->l_pflag, LP_SINGLESTEP) || req == PT_STEP);
   1391       1.1  pgoyette 		if (error)
   1392       1.1  pgoyette 			break;
   1393       1.1  pgoyette #endif
   1394       1.1  pgoyette 		if (req == PT_DETACH) {
   1395      1.23     kamil 			CLR(t->p_slflag, PSL_TRACED|PSL_SYSCALL);
   1396       1.1  pgoyette 
   1397       1.1  pgoyette 			/* give process back to original parent or init */
   1398       1.1  pgoyette 			if (t->p_opptr != t->p_pptr) {
   1399       1.1  pgoyette 				struct proc *pp = t->p_opptr;
   1400       1.1  pgoyette 				proc_reparent(t, pp ? pp : initproc);
   1401       1.1  pgoyette 			}
   1402       1.1  pgoyette 
   1403       1.1  pgoyette 			/* not being traced any more */
   1404       1.1  pgoyette 			t->p_opptr = NULL;
   1405      1.21     kamil 
   1406      1.21     kamil 			/* clear single step */
   1407      1.21     kamil 			LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
   1408      1.21     kamil 				CLR(lt2->l_pflag, LP_SINGLESTEP);
   1409      1.21     kamil 			}
   1410      1.21     kamil 			CLR(lt->l_pflag, LP_SINGLESTEP);
   1411       1.1  pgoyette 		}
   1412       1.1  pgoyette 	sendsig:
   1413  1.45.2.2    martin 		error = ptrace_sendsig(l, req, t, lt, signo, resume_all);
   1414       1.1  pgoyette 		break;
   1415       1.1  pgoyette 
   1416      1.28  christos 	case PT_SYSCALLEMU:
   1417       1.1  pgoyette 		if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
   1418       1.1  pgoyette 			error = EINVAL;
   1419       1.1  pgoyette 			break;
   1420       1.1  pgoyette 		}
   1421       1.1  pgoyette 		SET(t->p_slflag, PSL_SYSCALLEMU);
   1422       1.1  pgoyette 		break;
   1423       1.1  pgoyette 
   1424      1.21     kamil #ifdef PT_STEP
   1425      1.28  christos 	case PT_SETSTEP:
   1426      1.21     kamil 		write = 1;
   1427      1.21     kamil 
   1428  1.45.2.1  christos 		/* FALLTHROUGH */
   1429      1.28  christos 	case PT_CLEARSTEP:
   1430      1.21     kamil 		/* write = 0 done above. */
   1431      1.27  christos 		if ((error = ptrace_update_lwp(t, &lt, data)) != 0)
   1432      1.27  christos 			break;
   1433      1.21     kamil 
   1434      1.27  christos 		if (write)
   1435      1.21     kamil 			SET(lt->l_pflag, LP_SINGLESTEP);
   1436      1.21     kamil 		else
   1437      1.21     kamil 			CLR(lt->l_pflag, LP_SINGLESTEP);
   1438      1.21     kamil 		break;
   1439      1.21     kamil #endif
   1440      1.21     kamil 
   1441      1.28  christos 	case PT_KILL:
   1442       1.1  pgoyette 		/* just send the process a KILL signal. */
   1443       1.1  pgoyette 		signo = SIGKILL;
   1444       1.1  pgoyette 		goto sendsig;	/* in PT_CONTINUE, above. */
   1445       1.1  pgoyette 
   1446  1.45.2.2    martin 	case PT_STOP:
   1447  1.45.2.2    martin 		/* just send the process a STOP signal. */
   1448  1.45.2.2    martin 		signo = SIGSTOP;
   1449  1.45.2.2    martin 		goto sendsig;	/* in PT_CONTINUE, above. */
   1450  1.45.2.2    martin 
   1451      1.28  christos 	case PT_ATTACH:
   1452       1.1  pgoyette 		/*
   1453       1.1  pgoyette 		 * Go ahead and set the trace flag.
   1454       1.1  pgoyette 		 * Save the old parent (it's reset in
   1455       1.1  pgoyette 		 *   _DETACH, and also in kern_exit.c:wait4()
   1456       1.1  pgoyette 		 * Reparent the process so that the tracing
   1457       1.1  pgoyette 		 *   proc gets to see all the action.
   1458       1.1  pgoyette 		 * Stop the target.
   1459       1.1  pgoyette 		 */
   1460       1.2  christos 		proc_changeparent(t, p);
   1461       1.4  christos 		signo = SIGSTOP;
   1462       1.1  pgoyette 		goto sendsig;
   1463       1.1  pgoyette 
   1464      1.28  christos 	case PT_GET_EVENT_MASK:
   1465      1.27  christos 		error = ptrace_get_event_mask(t, addr, data);
   1466       1.1  pgoyette 		break;
   1467       1.1  pgoyette 
   1468      1.28  christos 	case PT_SET_EVENT_MASK:
   1469      1.27  christos 		error = ptrace_set_event_mask(t, addr, data);
   1470       1.1  pgoyette 		break;
   1471       1.1  pgoyette 
   1472      1.28  christos 	case PT_GET_PROCESS_STATE:
   1473      1.27  christos 		error = ptrace_get_process_state(t, addr, data);
   1474       1.1  pgoyette 		break;
   1475       1.1  pgoyette 
   1476       1.1  pgoyette 	case PT_LWPINFO:
   1477      1.27  christos 		error = ptrace_lwpinfo(t, &lt, addr, data);
   1478       1.1  pgoyette 		break;
   1479       1.1  pgoyette 
   1480      1.28  christos 	case PT_SET_SIGINFO:
   1481      1.29  christos 		error = ptrace_set_siginfo(t, &lt, ptm, addr, data);
   1482       1.8     kamil 		break;
   1483       1.8     kamil 
   1484      1.28  christos 	case PT_GET_SIGINFO:
   1485      1.29  christos 		error = ptrace_get_siginfo(t, ptm, addr, data);
   1486       1.8     kamil 		break;
   1487       1.8     kamil 
   1488      1.28  christos 	case PT_RESUME:
   1489      1.28  christos 	case PT_SUSPEND:
   1490      1.27  christos 		error = ptrace_startstop(t, &lt, req, addr, data);
   1491      1.15     kamil 		break;
   1492      1.15     kamil 
   1493  1.45.2.2    martin 	case PT_LWPSTATUS:
   1494  1.45.2.2    martin 		error = ptrace_lwpstatus(t, ptm, &lt, addr, data, false);
   1495  1.45.2.2    martin 		break;
   1496  1.45.2.2    martin 
   1497  1.45.2.2    martin 	case PT_LWPNEXT:
   1498  1.45.2.2    martin 		error = ptrace_lwpstatus(t, ptm, &lt, addr, data, true);
   1499  1.45.2.2    martin 		break;
   1500  1.45.2.2    martin 
   1501      1.28  christos #ifdef PT_REGISTERS
   1502      1.28  christos 	case_PT_SETREGS
   1503      1.28  christos 	case_PT_GETREGS
   1504      1.28  christos 	case_PT_SETFPREGS
   1505      1.28  christos 	case_PT_GETFPREGS
   1506      1.28  christos 	case_PT_SETDBREGS
   1507      1.28  christos 	case_PT_GETDBREGS
   1508      1.27  christos 		error = ptrace_regs(l, &lt, req, ptm, addr, data);
   1509       1.7     kamil 		break;
   1510       1.7     kamil #endif
   1511       1.7     kamil 
   1512       1.1  pgoyette #ifdef __HAVE_PTRACE_MACHDEP
   1513       1.1  pgoyette 	PTRACE_MACHDEP_REQUEST_CASES
   1514       1.1  pgoyette 		error = ptrace_machdep_dorequest(l, lt, req, addr, data);
   1515       1.1  pgoyette 		break;
   1516       1.1  pgoyette #endif
   1517       1.1  pgoyette 	}
   1518       1.1  pgoyette 
   1519      1.27  christos out:
   1520       1.1  pgoyette 	if (pheld) {
   1521       1.1  pgoyette 		mutex_exit(t->p_lock);
   1522       1.1  pgoyette 		mutex_exit(proc_lock);
   1523       1.1  pgoyette 	}
   1524       1.1  pgoyette 	if (lt != NULL)
   1525       1.1  pgoyette 		lwp_delref(lt);
   1526  1.45.2.2    martin 	if (locked)
   1527  1.45.2.2    martin 		rw_exit(&t->p_reflock);
   1528       1.1  pgoyette 
   1529       1.1  pgoyette 	return error;
   1530       1.1  pgoyette }
   1531       1.1  pgoyette 
   1532      1.32  christos typedef int (*regrfunc_t)(struct lwp *, void *, size_t *);
   1533      1.32  christos typedef int (*regwfunc_t)(struct lwp *, void *, size_t);
   1534      1.24  christos 
   1535      1.28  christos #ifdef PT_REGISTERS
   1536      1.24  christos static int
   1537      1.32  christos proc_regio(struct lwp *l, struct uio *uio, size_t ks, regrfunc_t r,
   1538      1.32  christos     regwfunc_t w)
   1539       1.1  pgoyette {
   1540      1.24  christos 	char buf[1024];
   1541       1.1  pgoyette 	int error;
   1542       1.1  pgoyette 	char *kv;
   1543      1.32  christos 	size_t kl;
   1544       1.1  pgoyette 
   1545      1.32  christos 	if (ks > sizeof(buf))
   1546      1.24  christos 		return E2BIG;
   1547      1.24  christos 
   1548      1.32  christos 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)ks)
   1549       1.1  pgoyette 		return EINVAL;
   1550       1.1  pgoyette 
   1551      1.24  christos 	kv = buf + uio->uio_offset;
   1552      1.32  christos 	kl = ks - uio->uio_offset;
   1553       1.1  pgoyette 
   1554      1.24  christos 	if (kl > uio->uio_resid)
   1555       1.1  pgoyette 		kl = uio->uio_resid;
   1556       1.1  pgoyette 
   1557      1.32  christos 	error = (*r)(l, buf, &ks);
   1558       1.1  pgoyette 	if (error == 0)
   1559       1.1  pgoyette 		error = uiomove(kv, kl, uio);
   1560       1.1  pgoyette 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1561       1.1  pgoyette 		if (l->l_stat != LSSTOP)
   1562       1.1  pgoyette 			error = EBUSY;
   1563       1.1  pgoyette 		else
   1564      1.32  christos 			error = (*w)(l, buf, ks);
   1565       1.1  pgoyette 	}
   1566       1.1  pgoyette 
   1567       1.1  pgoyette 	uio->uio_offset = 0;
   1568       1.1  pgoyette 	return error;
   1569      1.24  christos }
   1570      1.24  christos #endif
   1571      1.24  christos 
   1572      1.24  christos int
   1573      1.24  christos process_doregs(struct lwp *curl /*tracer*/,
   1574      1.24  christos     struct lwp *l /*traced*/,
   1575      1.24  christos     struct uio *uio)
   1576      1.24  christos {
   1577      1.24  christos #if defined(PT_GETREGS) || defined(PT_SETREGS)
   1578      1.24  christos 	size_t s;
   1579      1.32  christos 	regrfunc_t r;
   1580      1.32  christos 	regwfunc_t w;
   1581      1.24  christos 
   1582      1.24  christos #ifdef COMPAT_NETBSD32
   1583  1.45.2.2    martin 	const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
   1584      1.24  christos 
   1585      1.24  christos 	if (__predict_false(pk32)) {
   1586  1.45.2.2    martin 		if ((l->l_proc->p_flag & PK_32) == 0) {
   1587  1.45.2.2    martin 			// 32 bit tracer can't trace 64 bit process
   1588  1.45.2.2    martin 			return EINVAL;
   1589  1.45.2.2    martin 		}
   1590      1.26  christos 		s = sizeof(process_reg32);
   1591  1.45.2.2    martin 		r = __FPTRCAST(regrfunc_t, process_read_regs32);
   1592  1.45.2.2    martin 		w = __FPTRCAST(regwfunc_t, process_write_regs32);
   1593      1.24  christos 	} else
   1594      1.24  christos #endif
   1595      1.24  christos 	{
   1596      1.24  christos 		s = sizeof(struct reg);
   1597  1.45.2.2    martin 		r = __FPTRCAST(regrfunc_t, process_read_regs);
   1598  1.45.2.2    martin 		w = __FPTRCAST(regwfunc_t, process_write_regs);
   1599      1.24  christos 	}
   1600      1.24  christos 	return proc_regio(l, uio, s, r, w);
   1601       1.1  pgoyette #else
   1602       1.1  pgoyette 	return EINVAL;
   1603       1.1  pgoyette #endif
   1604       1.1  pgoyette }
   1605       1.1  pgoyette 
   1606       1.1  pgoyette int
   1607       1.1  pgoyette process_validregs(struct lwp *l)
   1608       1.1  pgoyette {
   1609       1.1  pgoyette 
   1610       1.1  pgoyette #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1611       1.1  pgoyette 	return (l->l_flag & LW_SYSTEM) == 0;
   1612       1.1  pgoyette #else
   1613       1.1  pgoyette 	return 0;
   1614       1.1  pgoyette #endif
   1615       1.1  pgoyette }
   1616       1.1  pgoyette 
   1617       1.1  pgoyette int
   1618       1.1  pgoyette process_dofpregs(struct lwp *curl /*tracer*/,
   1619       1.1  pgoyette     struct lwp *l /*traced*/,
   1620       1.1  pgoyette     struct uio *uio)
   1621       1.1  pgoyette {
   1622       1.1  pgoyette #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
   1623      1.24  christos 	size_t s;
   1624      1.32  christos 	regrfunc_t r;
   1625      1.32  christos 	regwfunc_t w;
   1626       1.1  pgoyette 
   1627      1.24  christos #ifdef COMPAT_NETBSD32
   1628  1.45.2.2    martin 	const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
   1629       1.1  pgoyette 
   1630      1.24  christos 	if (__predict_false(pk32)) {
   1631  1.45.2.2    martin 		if ((l->l_proc->p_flag & PK_32) == 0) {
   1632  1.45.2.2    martin 			// 32 bit tracer can't trace 64 bit process
   1633  1.45.2.2    martin 			return EINVAL;
   1634  1.45.2.2    martin 		}
   1635      1.26  christos 		s = sizeof(process_fpreg32);
   1636      1.32  christos 		r = (regrfunc_t)process_read_fpregs32;
   1637      1.32  christos 		w = (regwfunc_t)process_write_fpregs32;
   1638      1.24  christos 	} else
   1639      1.24  christos #endif
   1640      1.24  christos 	{
   1641      1.24  christos 		s = sizeof(struct fpreg);
   1642      1.32  christos 		r = (regrfunc_t)process_read_fpregs;
   1643      1.32  christos 		w = (regwfunc_t)process_write_fpregs;
   1644       1.1  pgoyette 	}
   1645      1.24  christos 	return proc_regio(l, uio, s, r, w);
   1646       1.1  pgoyette #else
   1647       1.1  pgoyette 	return EINVAL;
   1648       1.1  pgoyette #endif
   1649       1.1  pgoyette }
   1650       1.1  pgoyette 
   1651       1.1  pgoyette int
   1652       1.1  pgoyette process_validfpregs(struct lwp *l)
   1653       1.1  pgoyette {
   1654       1.1  pgoyette 
   1655       1.1  pgoyette #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1656       1.1  pgoyette 	return (l->l_flag & LW_SYSTEM) == 0;
   1657       1.1  pgoyette #else
   1658       1.1  pgoyette 	return 0;
   1659       1.1  pgoyette #endif
   1660       1.1  pgoyette }
   1661       1.1  pgoyette 
   1662      1.17     kamil int
   1663      1.17     kamil process_dodbregs(struct lwp *curl /*tracer*/,
   1664      1.17     kamil     struct lwp *l /*traced*/,
   1665      1.17     kamil     struct uio *uio)
   1666      1.17     kamil {
   1667      1.17     kamil #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
   1668      1.24  christos 	size_t s;
   1669      1.32  christos 	regrfunc_t r;
   1670      1.32  christos 	regwfunc_t w;
   1671      1.17     kamil 
   1672      1.24  christos #ifdef COMPAT_NETBSD32
   1673  1.45.2.2    martin 	const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
   1674      1.17     kamil 
   1675      1.24  christos 	if (__predict_false(pk32)) {
   1676  1.45.2.2    martin 		if ((l->l_proc->p_flag & PK_32) == 0) {
   1677  1.45.2.2    martin 			// 32 bit tracer can't trace 64 bit process
   1678  1.45.2.2    martin 			return EINVAL;
   1679  1.45.2.2    martin 		}
   1680      1.26  christos 		s = sizeof(process_dbreg32);
   1681      1.32  christos 		r = (regrfunc_t)process_read_dbregs32;
   1682      1.32  christos 		w = (regwfunc_t)process_write_dbregs32;
   1683      1.24  christos 	} else
   1684      1.24  christos #endif
   1685      1.24  christos 	{
   1686      1.24  christos 		s = sizeof(struct dbreg);
   1687      1.32  christos 		r = (regrfunc_t)process_read_dbregs;
   1688      1.32  christos 		w = (regwfunc_t)process_write_dbregs;
   1689      1.17     kamil 	}
   1690      1.24  christos 	return proc_regio(l, uio, s, r, w);
   1691      1.17     kamil #else
   1692      1.17     kamil 	return EINVAL;
   1693      1.17     kamil #endif
   1694      1.17     kamil }
   1695      1.17     kamil 
   1696      1.17     kamil int
   1697      1.17     kamil process_validdbregs(struct lwp *l)
   1698      1.17     kamil {
   1699      1.17     kamil 
   1700      1.17     kamil #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1701      1.17     kamil 	return (l->l_flag & LW_SYSTEM) == 0;
   1702      1.17     kamil #else
   1703      1.17     kamil 	return 0;
   1704      1.17     kamil #endif
   1705      1.17     kamil }
   1706      1.17     kamil 
   1707       1.1  pgoyette static int
   1708       1.1  pgoyette process_auxv_offset(struct proc *p, struct uio *uio)
   1709       1.1  pgoyette {
   1710       1.1  pgoyette 	struct ps_strings pss;
   1711       1.1  pgoyette 	int error;
   1712       1.1  pgoyette 	off_t off = (off_t)p->p_psstrp;
   1713       1.1  pgoyette 
   1714       1.1  pgoyette 	if ((error = copyin_psstrings(p, &pss)) != 0)
   1715       1.1  pgoyette 		return error;
   1716       1.1  pgoyette 
   1717       1.1  pgoyette 	if (pss.ps_envstr == NULL)
   1718       1.1  pgoyette 		return EIO;
   1719       1.1  pgoyette 
   1720  1.45.2.2    martin #ifdef COMPAT_NETBSD32
   1721  1.45.2.2    martin 	if (p->p_flag & PK_32)
   1722  1.45.2.2    martin 		uio->uio_offset += (off_t)((vaddr_t)pss.ps_envstr +
   1723  1.45.2.2    martin 		    sizeof(uint32_t) * (pss.ps_nenvstr + 1));
   1724  1.45.2.2    martin 	else
   1725  1.45.2.2    martin #endif
   1726  1.45.2.2    martin 		uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr +
   1727  1.45.2.2    martin 		    pss.ps_nenvstr + 1);
   1728  1.45.2.2    martin 
   1729       1.1  pgoyette #ifdef __MACHINE_STACK_GROWS_UP
   1730       1.1  pgoyette 	if (uio->uio_offset < off)
   1731       1.1  pgoyette 		return EIO;
   1732       1.1  pgoyette #else
   1733       1.1  pgoyette 	if (uio->uio_offset > off)
   1734       1.1  pgoyette 		return EIO;
   1735       1.1  pgoyette 	if ((uio->uio_offset + uio->uio_resid) > off)
   1736       1.1  pgoyette 		uio->uio_resid = off - uio->uio_offset;
   1737       1.1  pgoyette #endif
   1738       1.1  pgoyette 	return 0;
   1739       1.1  pgoyette }
   1740       1.1  pgoyette #endif /* PTRACE */
   1741       1.1  pgoyette 
   1742  1.45.2.1  christos MODULE(MODULE_CLASS_EXEC, ptrace_common, NULL);
   1743       1.1  pgoyette 
   1744       1.1  pgoyette static int
   1745       1.1  pgoyette ptrace_common_modcmd(modcmd_t cmd, void *arg)
   1746       1.1  pgoyette {
   1747       1.1  pgoyette         int error;
   1748       1.1  pgoyette 
   1749       1.1  pgoyette         switch (cmd) {
   1750       1.1  pgoyette         case MODULE_CMD_INIT:
   1751       1.1  pgoyette                 error = ptrace_init();
   1752       1.1  pgoyette                 break;
   1753       1.1  pgoyette         case MODULE_CMD_FINI:
   1754       1.1  pgoyette                 error = ptrace_fini();
   1755       1.1  pgoyette                 break;
   1756       1.1  pgoyette         default:
   1757       1.1  pgoyette 		ptrace_hooks();
   1758       1.1  pgoyette                 error = ENOTTY;
   1759       1.1  pgoyette                 break;
   1760       1.1  pgoyette         }
   1761       1.1  pgoyette         return error;
   1762       1.1  pgoyette }
   1763