summaryrefslogtreecommitdiff
path: root/sys/compat/netbsd32/netbsd32_ptrace.c
blob: 74ad2dcd844b2d7857ac9c66f061310a5c5a7e42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*	$NetBSD: netbsd32_ptrace.c,v 1.9 2021/09/07 11:43:05 riastradh Exp $	*/

/*
 * Copyright (c) 2016 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Nick Hudson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_ptrace.c,v 1.9 2021/09/07 11:43:05 riastradh Exp $");

#if defined(_KERNEL_OPT)
#include "opt_ptrace.h"
#include "opt_compat_netbsd.h"
#endif

#include <sys/param.h>
#include <sys/module.h>
#include <sys/ptrace.h>
#include <sys/syscallvar.h>

#include <compat/netbsd32/netbsd32.h>
#include <compat/netbsd32/netbsd32_syscall.h>
#include <compat/netbsd32/netbsd32_syscallargs.h>
#include <compat/netbsd32/netbsd32_conv.h>

#ifndef PTRACE_TRANSLATE_REQUEST32
#define PTRACE_TRANSLATE_REQUEST32(x) x
#endif

static void
netbsd32_lwpstatus_to_lwpstatus32(struct netbsd32_ptrace_lwpstatus *pls32,
    const struct ptrace_lwpstatus *pls)
{
	memset(pls32, 0, sizeof(*pls32));
	pls32->pl_lwpid = pls->pl_lwpid;
	pls32->pl_sigpend = pls->pl_sigpend;
	pls32->pl_sigmask = pls->pl_sigmask;
	memcpy(&pls32->pl_name, &pls->pl_name, PL_LNAMELEN);
	NETBSD32PTR32(pls32->pl_private, pls->pl_private);
}

void
netbsd32_read_lwpstatus(struct lwp *l, struct netbsd32_ptrace_lwpstatus *pls32)
{
	struct ptrace_lwpstatus pls;

	process_read_lwpstatus(l, &pls);

	netbsd32_lwpstatus_to_lwpstatus32(pls32, &pls);
}

/*
 * PTRACE methods
 */

static int
netbsd32_copyin_piod(struct ptrace_io_desc *piod, const void *addr, size_t len)
{
	struct netbsd32_ptrace_io_desc piod32;

	if (len != 0 && sizeof(piod32) != len)
		return EINVAL;

	int error = copyin(addr, &piod32, sizeof(piod32));
	if (error)
		return error;
	piod->piod_op = piod32.piod_op;
	piod->piod_offs = NETBSD32PTR64(piod32.piod_offs);
	piod->piod_addr = NETBSD32PTR64(piod32.piod_addr);
	piod->piod_len = (size_t)piod32.piod_len;

	return 0;
}

static int
netbsd32_copyout_piod(const struct ptrace_io_desc *piod, void *addr, size_t len)
{
	struct netbsd32_ptrace_io_desc piod32;

	if (len != 0 && sizeof(piod32) != len)
		return EINVAL;

	memset(&piod32, 0, sizeof(piod32));
	piod32.piod_op = piod->piod_op;
	NETBSD32PTR32(piod32.piod_offs, piod->piod_offs);
	NETBSD32PTR32(piod32.piod_addr, piod->piod_addr);
	piod32.piod_len = (netbsd32_size_t)piod->piod_len;
	return copyout(&piod32, addr, sizeof(piod32));
}

static int
netbsd32_copyin_siginfo(struct ptrace_siginfo *psi, const void *addr, size_t len)
{
	struct netbsd32_ptrace_siginfo psi32;

	if (sizeof(psi32) != len)
		return EINVAL;

	int error = copyin(addr, &psi32, sizeof(psi32));
	if (error)
		return error;
	psi->psi_lwpid = psi32.psi_lwpid;
	netbsd32_si32_to_si(&psi->psi_siginfo, &psi32.psi_siginfo);
	return 0;
}

static int
netbsd32_copyout_siginfo(const struct ptrace_siginfo *psi, void *addr, size_t len)
{
	struct netbsd32_ptrace_siginfo psi32;

	if (sizeof(psi32) != len)
		return EINVAL;

	memset(&psi32, 0, sizeof(psi32));
	psi32.psi_lwpid = psi->psi_lwpid;
	netbsd32_si_to_si32(&psi32.psi_siginfo, &psi->psi_siginfo);
	return copyout(&psi32, addr, sizeof(psi32));
}

static int
netbsd32_copyout_lwpstatus(const struct ptrace_lwpstatus *pls, void *addr, size_t len)
{
	struct netbsd32_ptrace_lwpstatus pls32;

	if (len > sizeof(pls32))
		return EINVAL;

	netbsd32_lwpstatus_to_lwpstatus32(&pls32, pls);

	return copyout(&pls32, addr, MIN(len, sizeof(pls32)));
}

static int
netbsd32_doregs(struct lwp *curl /*tracer*/,
    struct lwp *l /*traced*/,
    struct uio *uio)
{
#if defined(PT_GETREGS) || defined(PT_SETREGS)
	process_reg32 r32;
	int error;
	char *kv;
	int kl;

	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
		return EINVAL;

	kl = sizeof(r32);
	kv = (char *)&r32;

	kv += uio->uio_offset;
	kl -= uio->uio_offset;
	if ((size_t)kl > uio->uio_resid)
		kl = uio->uio_resid;
	error = process_read_regs32(l, &r32);
	if (error == 0)
		error = uiomove(kv, kl, uio);
	if (error == 0 && uio->uio_rw == UIO_WRITE) {
		if (l->l_stat != LSSTOP)
			error = EBUSY;
		else
			error = process_write_regs32(l, &r32);
	}

	uio->uio_offset = 0;
	return error;
#else
	return EINVAL;
#endif
}

static int
netbsd32_dofpregs(struct lwp *curl /*tracer*/,
    struct lwp *l /*traced*/,
    struct uio *uio)
{
#if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
	process_fpreg32 r32;
	int error;
	char *kv;
	size_t kl;

	KASSERT(l->l_proc->p_flag & PK_32);
	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
		return EINVAL;
	kl = sizeof(r32);
	kv = (char *)&r32;

	kv += uio->uio_offset;
	kl -= uio->uio_offset;
	if (kl > uio->uio_resid)
		kl = uio->uio_resid;

	error = process_read_fpregs32(l, &r32, &kl);
	if (error == 0)
		error = uiomove(kv, kl, uio);
	if (error == 0 && uio->uio_rw == UIO_WRITE) {
		if (l->l_stat != LSSTOP)
			error = EBUSY;
		else
			error = process_write_fpregs32(l, &r32, kl);
	}
	uio->uio_offset = 0;
	return error;
#else
	return EINVAL;
#endif
}

static int
netbsd32_dodbregs(struct lwp *curl /*tracer*/,
    struct lwp *l /*traced*/,
    struct uio *uio)
{
#if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
	process_dbreg32 r32;
	int error;
	char *kv;
	size_t kl;

	KASSERT(l->l_proc->p_flag & PK_32);
	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
		return EINVAL;
	kl = sizeof(r32);
	kv = (char *)&r32;

	kv += uio->uio_offset;
	kl -= uio->uio_offset;
	if (kl > uio->uio_resid)
		kl = uio->uio_resid;

	error = process_read_dbregs32(l, &r32, &kl);
	if (error == 0)
		error = uiomove(kv, kl, uio);
	if (error == 0 && uio->uio_rw == UIO_WRITE) {
		if (l->l_stat != LSSTOP)
			error = EBUSY;
		else
			error = process_write_dbregs32(l, &r32, kl);
	}
	uio->uio_offset = 0;
	return error;
#else
	return EINVAL;
#endif
}

static struct ptrace_methods netbsd32_ptm = {
	.ptm_copyin_piod = netbsd32_copyin_piod,
	.ptm_copyout_piod = netbsd32_copyout_piod,
	.ptm_copyin_siginfo = netbsd32_copyin_siginfo,
	.ptm_copyout_siginfo = netbsd32_copyout_siginfo,
	.ptm_copyout_lwpstatus = netbsd32_copyout_lwpstatus,
	.ptm_doregs = netbsd32_doregs,
	.ptm_dofpregs = netbsd32_dofpregs,
	.ptm_dodbregs = netbsd32_dodbregs
};


int
netbsd32_ptrace(struct lwp *l, const struct netbsd32_ptrace_args *uap,
    register_t *retval)
{
	int req;

	/* {
		syscallarg(int) req;
		syscallarg(pid_t) pid;
		syscallarg(netbsd32_voidp *) addr;
		syscallarg(int) data;
	} */

	req = PTRACE_TRANSLATE_REQUEST32(SCARG(uap, req));
	if (req == -1)
		return EOPNOTSUPP;

	return do_ptrace(&netbsd32_ptm, l, req, SCARG(uap, pid),
	    SCARG_P32(uap, addr), SCARG(uap, data), retval);
}

static const struct syscall_package compat_ptrace_syscalls[] = {
	{ NETBSD32_SYS_netbsd32_ptrace, 0, (sy_call_t *)netbsd32_ptrace },
	{ 0, 0, NULL },
};

#define	DEPS	"compat_netbsd32,ptrace_common"

MODULE(MODULE_CLASS_EXEC, compat_netbsd32_ptrace, DEPS);

static int
compat_netbsd32_ptrace_modcmd(modcmd_t cmd, void *arg)
{
	int error;

	switch (cmd) {
	case MODULE_CMD_INIT:
		error = syscall_establish(&emul_netbsd32,
		    compat_ptrace_syscalls);
		break;
	case MODULE_CMD_FINI:
		error = syscall_disestablish(&emul_netbsd32,
		    compat_ptrace_syscalls);
		break;
	default:
		error = ENOTTY;
		break;
	}
	return error;
}