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