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