Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_ptrace.c revision 1.7.2.1
      1 /*	$NetBSD: netbsd32_ptrace.c,v 1.7.2.1 2022/08/03 11:11:31 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: netbsd32_ptrace.c,v 1.7.2.1 2022/08/03 11:11:31 martin Exp $");
     34 
     35 #if defined(_KERNEL_OPT)
     36 #include "opt_ptrace.h"
     37 #include "opt_compat_netbsd.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/module.h>
     42 #include <sys/ptrace.h>
     43 #include <sys/syscallvar.h>
     44 
     45 #include <compat/netbsd32/netbsd32.h>
     46 #include <compat/netbsd32/netbsd32_syscall.h>
     47 #include <compat/netbsd32/netbsd32_syscallargs.h>
     48 #include <compat/netbsd32/netbsd32_conv.h>
     49 
     50 #ifndef PTRACE_TRANSLATE_REQUEST32
     51 #define PTRACE_TRANSLATE_REQUEST32(x) x
     52 #endif
     53 
     54 /*
     55  * PTRACE methods
     56  */
     57 
     58 static int
     59 netbsd32_copyin_piod(struct ptrace_io_desc *piod, const void *addr, size_t len)
     60 {
     61 	struct netbsd32_ptrace_io_desc piod32;
     62 
     63 	if (len != 0 && sizeof(piod32) != len)
     64 		return EINVAL;
     65 
     66 	int error = copyin(addr, &piod32, sizeof(piod32));
     67 	if (error)
     68 		return error;
     69 	piod->piod_op = piod32.piod_op;
     70 	piod->piod_offs = NETBSD32PTR64(piod32.piod_offs);
     71 	piod->piod_addr = NETBSD32PTR64(piod32.piod_addr);
     72 	piod->piod_len = (size_t)piod32.piod_len;
     73 
     74 	return 0;
     75 }
     76 
     77 static int
     78 netbsd32_copyout_piod(const struct ptrace_io_desc *piod, void *addr, size_t len)
     79 {
     80 	struct netbsd32_ptrace_io_desc piod32;
     81 
     82 	if (len != 0 && sizeof(piod32) != len)
     83 		return EINVAL;
     84 
     85 	memset(&piod32, 0, sizeof(piod32));
     86 	piod32.piod_op = piod->piod_op;
     87 	NETBSD32PTR32(piod32.piod_offs, piod->piod_offs);
     88 	NETBSD32PTR32(piod32.piod_addr, piod->piod_addr);
     89 	piod32.piod_len = (netbsd32_size_t)piod->piod_len;
     90 	return copyout(&piod32, addr, sizeof(piod32));
     91 }
     92 
     93 static int
     94 netbsd32_copyin_siginfo(struct ptrace_siginfo *psi, const void *addr, size_t len)
     95 {
     96 	struct netbsd32_ptrace_siginfo psi32;
     97 
     98 	if (sizeof(psi32) != len)
     99 		return EINVAL;
    100 
    101 	int error = copyin(addr, &psi32, sizeof(psi32));
    102 	if (error)
    103 		return error;
    104 	psi->psi_lwpid = psi32.psi_lwpid;
    105 	netbsd32_si32_to_si(&psi->psi_siginfo, &psi32.psi_siginfo);
    106 	return 0;
    107 }
    108 
    109 static int
    110 netbsd32_copyout_siginfo(const struct ptrace_siginfo *psi, void *addr, size_t len)
    111 {
    112 	struct netbsd32_ptrace_siginfo psi32;
    113 
    114 	if (sizeof(psi32) != len)
    115 		return EINVAL;
    116 
    117 	memset(&psi32, 0, sizeof(psi32));
    118 	psi32.psi_lwpid = psi->psi_lwpid;
    119 	netbsd32_si_to_si32(&psi32.psi_siginfo, &psi->psi_siginfo);
    120 	return copyout(&psi32, addr, sizeof(psi32));
    121 }
    122 
    123 static int
    124 netbsd32_doregs(struct lwp *curl /*tracer*/,
    125     struct lwp *l /*traced*/,
    126     struct uio *uio)
    127 {
    128 #if defined(PT_GETREGS) || defined(PT_SETREGS)
    129 	process_reg32 r32;
    130 	int error;
    131 	char *kv;
    132 	int kl;
    133 
    134 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
    135 		return EINVAL;
    136 
    137 	kl = sizeof(r32);
    138 	kv = (char *)&r32;
    139 
    140 	kv += uio->uio_offset;
    141 	kl -= uio->uio_offset;
    142 	if ((size_t)kl > uio->uio_resid)
    143 		kl = uio->uio_resid;
    144 	error = process_read_regs32(l, &r32);
    145 	if (error == 0)
    146 		error = uiomove(kv, kl, uio);
    147 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
    148 		if (l->l_stat != LSSTOP)
    149 			error = EBUSY;
    150 		else
    151 			error = process_write_regs32(l, &r32);
    152 	}
    153 
    154 	uio->uio_offset = 0;
    155 	return error;
    156 #else
    157 	return EINVAL;
    158 #endif
    159 }
    160 
    161 static int
    162 netbsd32_dofpregs(struct lwp *curl /*tracer*/,
    163     struct lwp *l /*traced*/,
    164     struct uio *uio)
    165 {
    166 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    167 	process_fpreg32 r32;
    168 	int error;
    169 	char *kv;
    170 	size_t kl;
    171 
    172 	KASSERT(l->l_proc->p_flag & PK_32);
    173 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
    174 		return EINVAL;
    175 	kl = sizeof(r32);
    176 	kv = (char *)&r32;
    177 
    178 	kv += uio->uio_offset;
    179 	kl -= uio->uio_offset;
    180 	if (kl > uio->uio_resid)
    181 		kl = uio->uio_resid;
    182 
    183 	error = process_read_fpregs32(l, &r32, &kl);
    184 	if (error == 0)
    185 		error = uiomove(kv, kl, uio);
    186 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
    187 		if (l->l_stat != LSSTOP)
    188 			error = EBUSY;
    189 		else
    190 			error = process_write_fpregs32(l, &r32, kl);
    191 	}
    192 	uio->uio_offset = 0;
    193 	return error;
    194 #else
    195 	return EINVAL;
    196 #endif
    197 }
    198 
    199 static int
    200 netbsd32_dodbregs(struct lwp *curl /*tracer*/,
    201     struct lwp *l /*traced*/,
    202     struct uio *uio)
    203 {
    204 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
    205 	process_dbreg32 r32;
    206 	int error;
    207 	char *kv;
    208 	size_t kl;
    209 
    210 	KASSERT(l->l_proc->p_flag & PK_32);
    211 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
    212 		return EINVAL;
    213 	kl = sizeof(r32);
    214 	kv = (char *)&r32;
    215 
    216 	kv += uio->uio_offset;
    217 	kl -= uio->uio_offset;
    218 	if (kl > uio->uio_resid)
    219 		kl = uio->uio_resid;
    220 
    221 	error = process_read_dbregs32(l, &r32, &kl);
    222 	if (error == 0)
    223 		error = uiomove(kv, kl, uio);
    224 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
    225 		if (l->l_stat != LSSTOP)
    226 			error = EBUSY;
    227 		else
    228 			error = process_write_dbregs32(l, &r32, kl);
    229 	}
    230 	uio->uio_offset = 0;
    231 	return error;
    232 #else
    233 	return EINVAL;
    234 #endif
    235 }
    236 
    237 static struct ptrace_methods netbsd32_ptm = {
    238 	.ptm_copyin_piod = netbsd32_copyin_piod,
    239 	.ptm_copyout_piod = netbsd32_copyout_piod,
    240 	.ptm_copyin_siginfo = netbsd32_copyin_siginfo,
    241 	.ptm_copyout_siginfo = netbsd32_copyout_siginfo,
    242 	.ptm_doregs = netbsd32_doregs,
    243 	.ptm_dofpregs = netbsd32_dofpregs,
    244 	.ptm_dodbregs = netbsd32_dodbregs
    245 };
    246 
    247 
    248 int
    249 netbsd32_ptrace(struct lwp *l, const struct netbsd32_ptrace_args *uap,
    250     register_t *retval)
    251 {
    252 	int req;
    253 
    254 	/* {
    255 		syscallarg(int) req;
    256 		syscallarg(pid_t) pid;
    257 		syscallarg(netbsd32_voidp *) addr;
    258 		syscallarg(int) data;
    259 	} */
    260 
    261 	req = PTRACE_TRANSLATE_REQUEST32(SCARG(uap, req));
    262 	if (req == -1)
    263 		return EOPNOTSUPP;
    264 
    265 	return do_ptrace(&netbsd32_ptm, l, req, SCARG(uap, pid),
    266 	    SCARG_P32(uap, addr), SCARG(uap, data), retval);
    267 }
    268 
    269 static const struct syscall_package compat_ptrace_syscalls[] = {
    270 	{ NETBSD32_SYS_netbsd32_ptrace, 0, (sy_call_t *)netbsd32_ptrace },
    271 	{ 0, 0, NULL },
    272 };
    273 
    274 #define	DEPS	"compat_netbsd32,ptrace_common"
    275 
    276 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_ptrace, DEPS);
    277 
    278 static int
    279 compat_netbsd32_ptrace_modcmd(modcmd_t cmd, void *arg)
    280 {
    281 	int error;
    282 
    283 	switch (cmd) {
    284 	case MODULE_CMD_INIT:
    285 		error = syscall_establish(&emul_netbsd32,
    286 		    compat_ptrace_syscalls);
    287 		break;
    288 	case MODULE_CMD_FINI:
    289 		error = syscall_disestablish(&emul_netbsd32,
    290 		    compat_ptrace_syscalls);
    291 		break;
    292 	default:
    293 		error = ENOTTY;
    294 		break;
    295 	}
    296 	return error;
    297 }
    298