netbsd32_ptrace.c revision 1.4 1 /* $NetBSD: netbsd32_ptrace.c,v 1.4 2017/02/23 03:34:22 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.4 2017/02/23 03:34:22 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 extern struct emul emul_netbsd32;
51
52
53 /*
54 * PTRACE methods
55 */
56
57 static int netbsd32_copyinpiod(struct ptrace_io_desc *, const void *);
58 static void netbsd32_copyoutpiod(const struct ptrace_io_desc *, void *);
59 static int netbsd32_doregs(struct lwp *, struct lwp *, struct uio *);
60 static int netbsd32_dofpregs(struct lwp *, struct lwp *, struct uio *);
61 static int netbsd32_dodbregs(struct lwp *, struct lwp *, struct uio *);
62
63
64 static int
65 netbsd32_copyinpiod(struct ptrace_io_desc *piod, const void *addr)
66 {
67 struct netbsd32_ptrace_io_desc piod32;
68
69 int error = copyin(addr, &piod32, sizeof(piod32));
70 if (error)
71 return error;
72 piod->piod_op = piod32.piod_op;
73 piod->piod_offs = NETBSD32PTR64(piod32.piod_offs);
74 piod->piod_addr = NETBSD32PTR64(piod32.piod_addr);
75 piod->piod_len = (size_t)piod32.piod_len;
76
77 return 0;
78 }
79
80 static void
81 netbsd32_copyoutpiod(const struct ptrace_io_desc *piod, void *addr)
82 {
83 struct netbsd32_ptrace_io_desc piod32;
84
85 piod32.piod_op = piod->piod_op;
86 NETBSD32PTR32(piod32.piod_offs, piod->piod_offs);
87 NETBSD32PTR32(piod32.piod_addr, piod->piod_addr);
88 piod32.piod_len = (netbsd32_size_t)piod->piod_len;
89 (void) copyout(&piod32, addr, sizeof(piod32));
90 }
91
92
93 static int
94 netbsd32_doregs(struct lwp *curl /*tracer*/,
95 struct lwp *l /*traced*/,
96 struct uio *uio)
97 {
98 #if defined(PT_GETREGS) || defined(PT_SETREGS)
99 process_reg32 r32;
100 int error;
101 char *kv;
102 int kl;
103
104 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
105 return EINVAL;
106
107 kl = sizeof(r32);
108 kv = (char *)&r32;
109
110 kv += uio->uio_offset;
111 kl -= uio->uio_offset;
112 if ((size_t)kl > uio->uio_resid)
113 kl = uio->uio_resid;
114 error = process_read_regs32(l, &r32);
115 if (error == 0)
116 error = uiomove(kv, kl, uio);
117 if (error == 0 && uio->uio_rw == UIO_WRITE) {
118 if (l->l_stat != LSSTOP)
119 error = EBUSY;
120 else
121 error = process_write_regs32(l, &r32);
122 }
123
124 uio->uio_offset = 0;
125 return error;
126 #else
127 return EINVAL;
128 #endif
129 }
130
131 static int
132 netbsd32_dofpregs(struct lwp *curl /*tracer*/,
133 struct lwp *l /*traced*/,
134 struct uio *uio)
135 {
136 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
137 process_fpreg32 r32;
138 int error;
139 char *kv;
140 size_t kl;
141
142 KASSERT(l->l_proc->p_flag & PK_32);
143 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
144 return EINVAL;
145 kl = sizeof(r32);
146 kv = (char *)&r32;
147
148 kv += uio->uio_offset;
149 kl -= uio->uio_offset;
150 if (kl > uio->uio_resid)
151 kl = uio->uio_resid;
152
153 error = process_read_fpregs32(l, &r32, &kl);
154 if (error == 0)
155 error = uiomove(kv, kl, uio);
156 if (error == 0 && uio->uio_rw == UIO_WRITE) {
157 if (l->l_stat != LSSTOP)
158 error = EBUSY;
159 else
160 error = process_write_fpregs32(l, &r32, kl);
161 }
162 uio->uio_offset = 0;
163 return error;
164 #else
165 return EINVAL;
166 #endif
167 }
168
169 static int
170 netbsd32_dodbregs(struct lwp *curl /*tracer*/,
171 struct lwp *l /*traced*/,
172 struct uio *uio)
173 {
174 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
175 process_dbreg32 r32;
176 int error;
177 char *kv;
178 size_t kl;
179
180 KASSERT(l->l_proc->p_flag & PK_32);
181 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
182 return EINVAL;
183 kl = sizeof(r32);
184 kv = (char *)&r32;
185
186 kv += uio->uio_offset;
187 kl -= uio->uio_offset;
188 if (kl > uio->uio_resid)
189 kl = uio->uio_resid;
190
191 error = process_read_dbregs32(l, &r32, &kl);
192 if (error == 0)
193 error = uiomove(kv, kl, uio);
194 if (error == 0 && uio->uio_rw == UIO_WRITE) {
195 if (l->l_stat != LSSTOP)
196 error = EBUSY;
197 else
198 error = process_write_dbregs32(l, &r32, kl);
199 }
200 uio->uio_offset = 0;
201 return error;
202 #else
203 return EINVAL;
204 #endif
205 }
206
207 static struct ptrace_methods netbsd32_ptm = {
208 .ptm_copyinpiod = netbsd32_copyinpiod,
209 .ptm_copyoutpiod = netbsd32_copyoutpiod,
210 .ptm_doregs = netbsd32_doregs,
211 .ptm_dofpregs = netbsd32_dofpregs,
212 .ptm_dodbregs = netbsd32_dodbregs
213 };
214
215
216 int
217 netbsd32_ptrace(struct lwp *l, const struct netbsd32_ptrace_args *uap,
218 register_t *retval)
219 {
220 /* {
221 syscallarg(int) req;
222 syscallarg(pid_t) pid;
223 syscallarg(netbsd32_voidp *) addr;
224 syscallarg(int) data;
225 } */
226
227 return do_ptrace(&netbsd32_ptm, l, SCARG(uap, req), SCARG(uap, pid),
228 SCARG_P32(uap, addr), SCARG(uap, data), retval);
229 }
230
231 static const struct syscall_package compat_ptrace_syscalls[] = {
232 { NETBSD32_SYS_netbsd32_ptrace, 0, (sy_call_t *)netbsd32_ptrace },
233 { 0, 0, NULL },
234 };
235
236 #define DEPS "compat_netbsd32,ptrace_common"
237
238 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_ptrace, DEPS);
239
240 static int
241 compat_netbsd32_ptrace_modcmd(modcmd_t cmd, void *arg)
242 {
243 int error;
244
245 switch (cmd) {
246 case MODULE_CMD_INIT:
247 error = syscall_establish(&emul_netbsd32,
248 compat_ptrace_syscalls);
249 break;
250 case MODULE_CMD_FINI:
251 error = syscall_disestablish(&emul_netbsd32,
252 compat_ptrace_syscalls);
253 break;
254 default:
255 error = ENOTTY;
256 break;
257 }
258 return error;
259 }
260