netbsd32_ptrace.c revision 1.3 1 /* $NetBSD: netbsd32_ptrace.c,v 1.3 2016/12/15 12:04:18 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.3 2016/12/15 12:04:18 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_dowatchpoint(struct lwp *, struct lwp *, int,
62 struct ptrace_watchpoint *, void *, register_t *);
63
64
65 static int
66 netbsd32_copyinpiod(struct ptrace_io_desc *piod, const void *addr)
67 {
68 struct netbsd32_ptrace_io_desc piod32;
69
70 int error = copyin(addr, &piod32, sizeof(piod32));
71 if (error)
72 return error;
73 piod->piod_op = piod32.piod_op;
74 piod->piod_offs = NETBSD32PTR64(piod32.piod_offs);
75 piod->piod_addr = NETBSD32PTR64(piod32.piod_addr);
76 piod->piod_len = (size_t)piod32.piod_len;
77
78 return 0;
79 }
80
81 static void
82 netbsd32_copyoutpiod(const struct ptrace_io_desc *piod, void *addr)
83 {
84 struct netbsd32_ptrace_io_desc piod32;
85
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 (void) copyout(&piod32, addr, sizeof(piod32));
91 }
92
93
94 static int
95 netbsd32_doregs(struct lwp *curl /*tracer*/,
96 struct lwp *l /*traced*/,
97 struct uio *uio)
98 {
99 #if defined(PT_GETREGS) || defined(PT_SETREGS)
100 process_reg32 r32;
101 int error;
102 char *kv;
103 int kl;
104
105 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
106 return EINVAL;
107
108 kl = sizeof(r32);
109 kv = (char *)&r32;
110
111 kv += uio->uio_offset;
112 kl -= uio->uio_offset;
113 if ((size_t)kl > uio->uio_resid)
114 kl = uio->uio_resid;
115 error = process_read_regs32(l, &r32);
116 if (error == 0)
117 error = uiomove(kv, kl, uio);
118 if (error == 0 && uio->uio_rw == UIO_WRITE) {
119 if (l->l_stat != LSSTOP)
120 error = EBUSY;
121 else
122 error = process_write_regs32(l, &r32);
123 }
124
125 uio->uio_offset = 0;
126 return error;
127 #else
128 return EINVAL;
129 #endif
130 }
131
132 static int
133 netbsd32_dofpregs(struct lwp *curl /*tracer*/,
134 struct lwp *l /*traced*/,
135 struct uio *uio)
136 {
137 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
138 process_fpreg32 r32;
139 int error;
140 char *kv;
141 size_t kl;
142
143 KASSERT(l->l_proc->p_flag & PK_32);
144 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r32))
145 return EINVAL;
146 kl = sizeof(r32);
147 kv = (char *)&r32;
148
149 kv += uio->uio_offset;
150 kl -= uio->uio_offset;
151 if (kl > uio->uio_resid)
152 kl = uio->uio_resid;
153
154 error = process_read_fpregs32(l, &r32, &kl);
155 if (error == 0)
156 error = uiomove(kv, kl, uio);
157 if (error == 0 && uio->uio_rw == UIO_WRITE) {
158 if (l->l_stat != LSSTOP)
159 error = EBUSY;
160 else
161 error = process_write_fpregs32(l, &r32, kl);
162 }
163 uio->uio_offset = 0;
164 return error;
165 #else
166 return EINVAL;
167 #endif
168 }
169
170 static int
171 netbsd32_dowatchpoint(struct lwp *curl /*tracer*/, struct lwp *l /*traced*/,
172 int write, struct ptrace_watchpoint *pw, void *addr, register_t *retval)
173 {
174
175 /* unimplemented */
176
177 return EINVAL;
178 }
179
180 static struct ptrace_methods netbsd32_ptm = {
181 .ptm_copyinpiod = netbsd32_copyinpiod,
182 .ptm_copyoutpiod = netbsd32_copyoutpiod,
183 .ptm_doregs = netbsd32_doregs,
184 .ptm_dofpregs = netbsd32_dofpregs,
185 .ptm_dowatchpoint = netbsd32_dowatchpoint
186 };
187
188
189 int
190 netbsd32_ptrace(struct lwp *l, const struct netbsd32_ptrace_args *uap,
191 register_t *retval)
192 {
193 /* {
194 syscallarg(int) req;
195 syscallarg(pid_t) pid;
196 syscallarg(netbsd32_voidp *) addr;
197 syscallarg(int) data;
198 } */
199
200 return do_ptrace(&netbsd32_ptm, l, SCARG(uap, req), SCARG(uap, pid),
201 SCARG_P32(uap, addr), SCARG(uap, data), retval);
202 }
203
204 static const struct syscall_package compat_ptrace_syscalls[] = {
205 { NETBSD32_SYS_netbsd32_ptrace, 0, (sy_call_t *)netbsd32_ptrace },
206 { 0, 0, NULL },
207 };
208
209 #define DEPS "compat_netbsd32,ptrace_common"
210
211 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_ptrace, DEPS);
212
213 static int
214 compat_netbsd32_ptrace_modcmd(modcmd_t cmd, void *arg)
215 {
216 int error;
217
218 switch (cmd) {
219 case MODULE_CMD_INIT:
220 error = syscall_establish(&emul_netbsd32,
221 compat_ptrace_syscalls);
222 break;
223 case MODULE_CMD_FINI:
224 error = syscall_disestablish(&emul_netbsd32,
225 compat_ptrace_syscalls);
226 break;
227 default:
228 error = ENOTTY;
229 break;
230 }
231 return error;
232 }
233