netbsd32_ptrace.c revision 1.8 1 /* $NetBSD: netbsd32_ptrace.c,v 1.8 2019/12/24 14:50:59 kamil 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.8 2019/12/24 14:50:59 kamil 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 static void
55 netbsd32_lwpstatus_to_lwpstatus32(struct netbsd32_ptrace_lwpstatus *pls32,
56 const struct ptrace_lwpstatus *pls)
57 {
58 pls32->pl_lwpid = pls->pl_lwpid;
59 pls32->pl_sigpend = pls->pl_sigpend;
60 pls32->pl_sigmask = pls->pl_sigmask;
61 memcpy(&pls32->pl_name, &pls->pl_name, PL_LNAMELEN);
62 NETBSD32PTR32(pls32->pl_private, pls->pl_private);
63 }
64
65 void
66 netbsd32_read_lwpstatus(struct lwp *l, struct netbsd32_ptrace_lwpstatus *pls32)
67 {
68 struct ptrace_lwpstatus pls;
69
70 process_read_lwpstatus(l, &pls);
71
72 netbsd32_lwpstatus_to_lwpstatus32(pls32, &pls);
73 }
74
75 /*
76 * PTRACE methods
77 */
78
79 static int
80 netbsd32_copyin_piod(struct ptrace_io_desc *piod, const void *addr, size_t len)
81 {
82 struct netbsd32_ptrace_io_desc piod32;
83
84 if (len != 0 && sizeof(piod32) != len)
85 return EINVAL;
86
87 int error = copyin(addr, &piod32, sizeof(piod32));
88 if (error)
89 return error;
90 piod->piod_op = piod32.piod_op;
91 piod->piod_offs = NETBSD32PTR64(piod32.piod_offs);
92 piod->piod_addr = NETBSD32PTR64(piod32.piod_addr);
93 piod->piod_len = (size_t)piod32.piod_len;
94
95 return 0;
96 }
97
98 static int
99 netbsd32_copyout_piod(const struct ptrace_io_desc *piod, void *addr, size_t len)
100 {
101 struct netbsd32_ptrace_io_desc piod32;
102
103 if (len != 0 && sizeof(piod32) != len)
104 return EINVAL;
105
106 piod32.piod_op = piod->piod_op;
107 NETBSD32PTR32(piod32.piod_offs, piod->piod_offs);
108 NETBSD32PTR32(piod32.piod_addr, piod->piod_addr);
109 piod32.piod_len = (netbsd32_size_t)piod->piod_len;
110 return copyout(&piod32, addr, sizeof(piod32));
111 }
112
113 static int
114 netbsd32_copyin_siginfo(struct ptrace_siginfo *psi, const void *addr, size_t len)
115 {
116 struct netbsd32_ptrace_siginfo psi32;
117
118 if (sizeof(psi32) != len)
119 return EINVAL;
120
121 int error = copyin(addr, &psi32, sizeof(psi32));
122 if (error)
123 return error;
124 psi->psi_lwpid = psi32.psi_lwpid;
125 netbsd32_si32_to_si(&psi->psi_siginfo, &psi32.psi_siginfo);
126 return 0;
127 }
128
129 static int
130 netbsd32_copyout_siginfo(const struct ptrace_siginfo *psi, void *addr, size_t len)
131 {
132 struct netbsd32_ptrace_siginfo psi32;
133
134 if (sizeof(psi32) != len)
135 return EINVAL;
136
137 psi32.psi_lwpid = psi->psi_lwpid;
138 netbsd32_si_to_si32(&psi32.psi_siginfo, &psi->psi_siginfo);
139 return copyout(&psi32, addr, sizeof(psi32));
140 }
141
142 static int
143 netbsd32_copyout_lwpstatus(const struct ptrace_lwpstatus *pls, void *addr, size_t len)
144 {
145 struct netbsd32_ptrace_lwpstatus pls32;
146
147 if (len > sizeof(pls32))
148 return EINVAL;
149
150 netbsd32_lwpstatus_to_lwpstatus32(&pls32, pls);
151
152 return copyout(&pls32, addr, MIN(len, sizeof(pls32)));
153 }
154
155 static int
156 netbsd32_doregs(struct lwp *curl /*tracer*/,
157 struct lwp *l /*traced*/,
158 struct uio *uio)
159 {
160 #if defined(PT_GETREGS) || defined(PT_SETREGS)
161 process_reg32 r32;
162 int error;
163 char *kv;
164 int kl;
165
166 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
167 return EINVAL;
168
169 kl = sizeof(r32);
170 kv = (char *)&r32;
171
172 kv += uio->uio_offset;
173 kl -= uio->uio_offset;
174 if ((size_t)kl > uio->uio_resid)
175 kl = uio->uio_resid;
176 error = process_read_regs32(l, &r32);
177 if (error == 0)
178 error = uiomove(kv, kl, uio);
179 if (error == 0 && uio->uio_rw == UIO_WRITE) {
180 if (l->l_stat != LSSTOP)
181 error = EBUSY;
182 else
183 error = process_write_regs32(l, &r32);
184 }
185
186 uio->uio_offset = 0;
187 return error;
188 #else
189 return EINVAL;
190 #endif
191 }
192
193 static int
194 netbsd32_dofpregs(struct lwp *curl /*tracer*/,
195 struct lwp *l /*traced*/,
196 struct uio *uio)
197 {
198 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
199 process_fpreg32 r32;
200 int error;
201 char *kv;
202 size_t kl;
203
204 KASSERT(l->l_proc->p_flag & PK_32);
205 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
206 return EINVAL;
207 kl = sizeof(r32);
208 kv = (char *)&r32;
209
210 kv += uio->uio_offset;
211 kl -= uio->uio_offset;
212 if (kl > uio->uio_resid)
213 kl = uio->uio_resid;
214
215 error = process_read_fpregs32(l, &r32, &kl);
216 if (error == 0)
217 error = uiomove(kv, kl, uio);
218 if (error == 0 && uio->uio_rw == UIO_WRITE) {
219 if (l->l_stat != LSSTOP)
220 error = EBUSY;
221 else
222 error = process_write_fpregs32(l, &r32, kl);
223 }
224 uio->uio_offset = 0;
225 return error;
226 #else
227 return EINVAL;
228 #endif
229 }
230
231 static int
232 netbsd32_dodbregs(struct lwp *curl /*tracer*/,
233 struct lwp *l /*traced*/,
234 struct uio *uio)
235 {
236 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
237 process_dbreg32 r32;
238 int error;
239 char *kv;
240 size_t kl;
241
242 KASSERT(l->l_proc->p_flag & PK_32);
243 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
244 return EINVAL;
245 kl = sizeof(r32);
246 kv = (char *)&r32;
247
248 kv += uio->uio_offset;
249 kl -= uio->uio_offset;
250 if (kl > uio->uio_resid)
251 kl = uio->uio_resid;
252
253 error = process_read_dbregs32(l, &r32, &kl);
254 if (error == 0)
255 error = uiomove(kv, kl, uio);
256 if (error == 0 && uio->uio_rw == UIO_WRITE) {
257 if (l->l_stat != LSSTOP)
258 error = EBUSY;
259 else
260 error = process_write_dbregs32(l, &r32, kl);
261 }
262 uio->uio_offset = 0;
263 return error;
264 #else
265 return EINVAL;
266 #endif
267 }
268
269 static struct ptrace_methods netbsd32_ptm = {
270 .ptm_copyin_piod = netbsd32_copyin_piod,
271 .ptm_copyout_piod = netbsd32_copyout_piod,
272 .ptm_copyin_siginfo = netbsd32_copyin_siginfo,
273 .ptm_copyout_siginfo = netbsd32_copyout_siginfo,
274 .ptm_copyout_lwpstatus = netbsd32_copyout_lwpstatus,
275 .ptm_doregs = netbsd32_doregs,
276 .ptm_dofpregs = netbsd32_dofpregs,
277 .ptm_dodbregs = netbsd32_dodbregs
278 };
279
280
281 int
282 netbsd32_ptrace(struct lwp *l, const struct netbsd32_ptrace_args *uap,
283 register_t *retval)
284 {
285 int req;
286
287 /* {
288 syscallarg(int) req;
289 syscallarg(pid_t) pid;
290 syscallarg(netbsd32_voidp *) addr;
291 syscallarg(int) data;
292 } */
293
294 req = PTRACE_TRANSLATE_REQUEST32(SCARG(uap, req));
295 if (req == -1)
296 return EOPNOTSUPP;
297
298 return do_ptrace(&netbsd32_ptm, l, req, SCARG(uap, pid),
299 SCARG_P32(uap, addr), SCARG(uap, data), retval);
300 }
301
302 static const struct syscall_package compat_ptrace_syscalls[] = {
303 { NETBSD32_SYS_netbsd32_ptrace, 0, (sy_call_t *)netbsd32_ptrace },
304 { 0, 0, NULL },
305 };
306
307 #define DEPS "compat_netbsd32,ptrace_common"
308
309 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_ptrace, DEPS);
310
311 static int
312 compat_netbsd32_ptrace_modcmd(modcmd_t cmd, void *arg)
313 {
314 int error;
315
316 switch (cmd) {
317 case MODULE_CMD_INIT:
318 error = syscall_establish(&emul_netbsd32,
319 compat_ptrace_syscalls);
320 break;
321 case MODULE_CMD_FINI:
322 error = syscall_disestablish(&emul_netbsd32,
323 compat_ptrace_syscalls);
324 break;
325 default:
326 error = ENOTTY;
327 break;
328 }
329 return error;
330 }
331