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