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