Sat Oct 15 09:09:55 2016 UTC ()
KNF


(skrll)
diff -r1.172 -r1.173 src/sys/kern/sys_process.c

cvs diff -r1.172 -r1.173 src/sys/kern/sys_process.c (switch to unified diff)

--- src/sys/kern/sys_process.c 2016/10/14 08:38:31 1.172
+++ src/sys/kern/sys_process.c 2016/10/15 09:09:55 1.173
@@ -1,1188 +1,1188 @@ @@ -1,1188 +1,1188 @@
1/* $NetBSD: sys_process.c,v 1.172 2016/10/14 08:38:31 skrll Exp $ */ 1/* $NetBSD: sys_process.c,v 1.173 2016/10/15 09:09:55 skrll Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 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 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32/*- 32/*-
33 * Copyright (c) 1982, 1986, 1989, 1993 33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved. 34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc. 35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed 36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph 37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc. 39 * the permission of UNIX System Laboratories, Inc.
40 * 40 *
41 * This code is derived from software contributed to Berkeley by 41 * This code is derived from software contributed to Berkeley by
42 * Jan-Simon Pendry. 42 * Jan-Simon Pendry.
43 * 43 *
44 * Redistribution and use in source and binary forms, with or without 44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions 45 * modification, are permitted provided that the following conditions
46 * are met: 46 * are met:
47 * 1. Redistributions of source code must retain the above copyright 47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer. 48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright 49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the 50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution. 51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors 52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software 53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission. 54 * without specific prior written permission.
55 * 55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE. 66 * SUCH DAMAGE.
67 * 67 *
68 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 68 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69 */ 69 */
70 70
71/*- 71/*-
72 * Copyright (c) 1993 Jan-Simon Pendry. 72 * Copyright (c) 1993 Jan-Simon Pendry.
73 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 73 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
74 * 74 *
75 * This code is derived from software contributed to Berkeley by 75 * This code is derived from software contributed to Berkeley by
76 * Jan-Simon Pendry. 76 * Jan-Simon Pendry.
77 * 77 *
78 * Redistribution and use in source and binary forms, with or without 78 * Redistribution and use in source and binary forms, with or without
79 * modification, are permitted provided that the following conditions 79 * modification, are permitted provided that the following conditions
80 * are met: 80 * are met:
81 * 1. Redistributions of source code must retain the above copyright 81 * 1. Redistributions of source code must retain the above copyright
82 * notice, this list of conditions and the following disclaimer. 82 * notice, this list of conditions and the following disclaimer.
83 * 2. Redistributions in binary form must reproduce the above copyright 83 * 2. Redistributions in binary form must reproduce the above copyright
84 * notice, this list of conditions and the following disclaimer in the 84 * notice, this list of conditions and the following disclaimer in the
85 * documentation and/or other materials provided with the distribution. 85 * documentation and/or other materials provided with the distribution.
86 * 3. All advertising materials mentioning features or use of this software 86 * 3. All advertising materials mentioning features or use of this software
87 * must display the following acknowledgement: 87 * must display the following acknowledgement:
88 * This product includes software developed by the University of 88 * This product includes software developed by the University of
89 * California, Berkeley and its contributors. 89 * California, Berkeley and its contributors.
90 * 4. Neither the name of the University nor the names of its contributors 90 * 4. Neither the name of the University nor the names of its contributors
91 * may be used to endorse or promote products derived from this software 91 * may be used to endorse or promote products derived from this software
92 * without specific prior written permission. 92 * without specific prior written permission.
93 * 93 *
94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 * SUCH DAMAGE. 104 * SUCH DAMAGE.
105 * 105 *
106 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 106 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
107 */ 107 */
108 108
109/* 109/*
110 * References: 110 * References:
111 * (1) Bach's "The Design of the UNIX Operating System", 111 * (1) Bach's "The Design of the UNIX Operating System",
112 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution, 112 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113 * (3) the "4.4BSD Programmer's Reference Manual" published 113 * (3) the "4.4BSD Programmer's Reference Manual" published
114 * by USENIX and O'Reilly & Associates. 114 * by USENIX and O'Reilly & Associates.
115 * The 4.4BSD PRM does a reasonably good job of documenting what the various 115 * The 4.4BSD PRM does a reasonably good job of documenting what the various
116 * ptrace() requests should actually do, and its text is quoted several times 116 * ptrace() requests should actually do, and its text is quoted several times
117 * in this file. 117 * in this file.
118 */ 118 */
119 119
120#include <sys/cdefs.h> 120#include <sys/cdefs.h>
121__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.172 2016/10/14 08:38:31 skrll Exp $"); 121__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.173 2016/10/15 09:09:55 skrll Exp $");
122 122
123#include "opt_ptrace.h" 123#include "opt_ptrace.h"
124#include "opt_ktrace.h" 124#include "opt_ktrace.h"
125#include "opt_pax.h" 125#include "opt_pax.h"
126 126
127#include <sys/param.h> 127#include <sys/param.h>
128#include <sys/systm.h> 128#include <sys/systm.h>
129#include <sys/proc.h> 129#include <sys/proc.h>
130#include <sys/errno.h> 130#include <sys/errno.h>
131#include <sys/exec.h> 131#include <sys/exec.h>
132#include <sys/pax.h> 132#include <sys/pax.h>
133#include <sys/ptrace.h> 133#include <sys/ptrace.h>
134#include <sys/uio.h> 134#include <sys/uio.h>
135#include <sys/ras.h> 135#include <sys/ras.h>
136#include <sys/kmem.h> 136#include <sys/kmem.h>
137#include <sys/kauth.h> 137#include <sys/kauth.h>
138#include <sys/mount.h> 138#include <sys/mount.h>
139#include <sys/syscallargs.h> 139#include <sys/syscallargs.h>
140 140
141#include <uvm/uvm_extern.h> 141#include <uvm/uvm_extern.h>
142 142
143#include <machine/reg.h> 143#include <machine/reg.h>
144 144
145#ifdef PTRACE 145#ifdef PTRACE
146 146
147# ifdef DEBUG 147# ifdef DEBUG
148# define DPRINTF(a) uprintf a 148# define DPRINTF(a) uprintf a
149# else 149# else
150# define DPRINTF(a) 150# define DPRINTF(a)
151# endif 151# endif
152 152
153static kauth_listener_t ptrace_listener; 153static kauth_listener_t ptrace_listener;
154#ifdef PTRACE 154#ifdef PTRACE
155static int process_auxv_offset(struct proc *, struct uio *); 155static int process_auxv_offset(struct proc *, struct uio *);
156#endif 156#endif
157 157
158static int 158static int
159ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 159ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
160 void *arg0, void *arg1, void *arg2, void *arg3) 160 void *arg0, void *arg1, void *arg2, void *arg3)
161{ 161{
162 struct proc *p; 162 struct proc *p;
163 int result; 163 int result;
164 164
165 result = KAUTH_RESULT_DEFER; 165 result = KAUTH_RESULT_DEFER;
166 p = arg0; 166 p = arg0;
167 167
168 if (action != KAUTH_PROCESS_PTRACE) 168 if (action != KAUTH_PROCESS_PTRACE)
169 return result; 169 return result;
170 170
171 switch ((u_long)arg1) { 171 switch ((u_long)arg1) {
172 case PT_TRACE_ME: 172 case PT_TRACE_ME:
173 case PT_ATTACH: 173 case PT_ATTACH:
174 case PT_WRITE_I: 174 case PT_WRITE_I:
175 case PT_WRITE_D: 175 case PT_WRITE_D:
176 case PT_READ_I: 176 case PT_READ_I:
177 case PT_READ_D: 177 case PT_READ_D:
178 case PT_IO: 178 case PT_IO:
179#ifdef PT_GETREGS 179#ifdef PT_GETREGS
180 case PT_GETREGS: 180 case PT_GETREGS:
181#endif 181#endif
182#ifdef PT_SETREGS 182#ifdef PT_SETREGS
183 case PT_SETREGS: 183 case PT_SETREGS:
184#endif 184#endif
185#ifdef PT_GETFPREGS 185#ifdef PT_GETFPREGS
186 case PT_GETFPREGS: 186 case PT_GETFPREGS:
187#endif 187#endif
188#ifdef PT_SETFPREGS 188#ifdef PT_SETFPREGS
189 case PT_SETFPREGS: 189 case PT_SETFPREGS:
190#endif 190#endif
191 case PT_SET_EVENT_MASK: 191 case PT_SET_EVENT_MASK:
192 case PT_GET_EVENT_MASK: 192 case PT_GET_EVENT_MASK:
193 case PT_GET_PROCESS_STATE: 193 case PT_GET_PROCESS_STATE:
194#ifdef __HAVE_PTRACE_MACHDEP 194#ifdef __HAVE_PTRACE_MACHDEP
195 PTRACE_MACHDEP_REQUEST_CASES 195 PTRACE_MACHDEP_REQUEST_CASES
196#endif 196#endif
197 if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) || 197 if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
198 ISSET(p->p_flag, PK_SUGID)) { 198 ISSET(p->p_flag, PK_SUGID)) {
199 break; 199 break;
200 } 200 }
201 201
202 result = KAUTH_RESULT_ALLOW; 202 result = KAUTH_RESULT_ALLOW;
203 203
204 break; 204 break;
205 205
206#ifdef PT_STEP 206#ifdef PT_STEP
207 case PT_STEP: 207 case PT_STEP:
208#endif 208#endif
209 case PT_CONTINUE: 209 case PT_CONTINUE:
210 case PT_KILL: 210 case PT_KILL:
211 case PT_DETACH: 211 case PT_DETACH:
212 case PT_LWPINFO: 212 case PT_LWPINFO:
213 case PT_SYSCALL: 213 case PT_SYSCALL:
214 case PT_SYSCALLEMU: 214 case PT_SYSCALLEMU:
215 case PT_DUMPCORE: 215 case PT_DUMPCORE:
216 result = KAUTH_RESULT_ALLOW; 216 result = KAUTH_RESULT_ALLOW;
217 break; 217 break;
218 218
219 default: 219 default:
220 break; 220 break;
221 } 221 }
222 222
223 return result; 223 return result;
224} 224}
225 225
226void 226void
227ptrace_init(void) 227ptrace_init(void)
228{ 228{
229 229
230 ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS, 230 ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
231 ptrace_listener_cb, NULL); 231 ptrace_listener_cb, NULL);
232} 232}
233 233
234/* 234/*
235 * Process debugging system call. 235 * Process debugging system call.
236 */ 236 */
237int 237int
238sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval) 238sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval)
239{ 239{
240 /* { 240 /* {
241 syscallarg(int) req; 241 syscallarg(int) req;
242 syscallarg(pid_t) pid; 242 syscallarg(pid_t) pid;
243 syscallarg(void *) addr; 243 syscallarg(void *) addr;
244 syscallarg(int) data; 244 syscallarg(int) data;
245 } */ 245 } */
246 struct proc *p = l->l_proc; 246 struct proc *p = l->l_proc;
247 struct lwp *lt; 247 struct lwp *lt;
248#ifdef PT_STEP 248#ifdef PT_STEP
249 struct lwp *lt2; 249 struct lwp *lt2;
250#endif 250#endif
251 struct proc *t; /* target process */ 251 struct proc *t; /* target process */
252 struct uio uio; 252 struct uio uio;
253 struct iovec iov; 253 struct iovec iov;
254 struct ptrace_io_desc piod; 254 struct ptrace_io_desc piod;
255 struct ptrace_event pe; 255 struct ptrace_event pe;
256 struct ptrace_state ps; 256 struct ptrace_state ps;
257 struct ptrace_lwpinfo pl; 257 struct ptrace_lwpinfo pl;
258 struct vmspace *vm; 258 struct vmspace *vm;
259 int error, write, tmp, req, pheld; 259 int error, write, tmp, req, pheld;
260 int signo = 0; 260 int signo = 0;
261 int resume_all; 261 int resume_all;
262 ksiginfo_t ksi; 262 ksiginfo_t ksi;
263 char *path; 263 char *path;
264 int len = 0; 264 int len = 0;
265 265
266 error = 0; 266 error = 0;
267 req = SCARG(uap, req); 267 req = SCARG(uap, req);
268 268
269 /* 269 /*
270 * If attaching or detaching, we need to get a write hold on the 270 * If attaching or detaching, we need to get a write hold on the
271 * proclist lock so that we can re-parent the target process. 271 * proclist lock so that we can re-parent the target process.
272 */ 272 */
273 mutex_enter(proc_lock); 273 mutex_enter(proc_lock);
274 274
275 /* "A foolish consistency..." XXX */ 275 /* "A foolish consistency..." XXX */
276 if (req == PT_TRACE_ME) { 276 if (req == PT_TRACE_ME) {
277 t = p; 277 t = p;
278 mutex_enter(t->p_lock); 278 mutex_enter(t->p_lock);
279 } else { 279 } else {
280 /* Find the process we're supposed to be operating on. */ 280 /* Find the process we're supposed to be operating on. */
281 t = proc_find(SCARG(uap, pid)); 281 t = proc_find(SCARG(uap, pid));
282 if (t == NULL) { 282 if (t == NULL) {
283 mutex_exit(proc_lock); 283 mutex_exit(proc_lock);
284 return (ESRCH); 284 return ESRCH;
285 } 285 }
286 286
287 /* XXX-elad */ 287 /* XXX-elad */
288 mutex_enter(t->p_lock); 288 mutex_enter(t->p_lock);
289 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, 289 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
290 t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 290 t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
291 if (error) { 291 if (error) {
292 mutex_exit(proc_lock); 292 mutex_exit(proc_lock);
293 mutex_exit(t->p_lock); 293 mutex_exit(t->p_lock);
294 return (ESRCH); 294 return ESRCH;
295 } 295 }
296 } 296 }
297 297
298 /* 298 /*
299 * Grab a reference on the process to prevent it from execing or 299 * Grab a reference on the process to prevent it from execing or
300 * exiting. 300 * exiting.
301 */ 301 */
302 if (!rw_tryenter(&t->p_reflock, RW_READER)) { 302 if (!rw_tryenter(&t->p_reflock, RW_READER)) {
303 mutex_exit(proc_lock); 303 mutex_exit(proc_lock);
304 mutex_exit(t->p_lock); 304 mutex_exit(t->p_lock);
305 return EBUSY; 305 return EBUSY;
306 } 306 }
307 307
308 /* Make sure we can operate on it. */ 308 /* Make sure we can operate on it. */
309 switch (req) { 309 switch (req) {
310 case PT_TRACE_ME: 310 case PT_TRACE_ME:
311 /* Saying that you're being traced is always legal. */ 311 /* Saying that you're being traced is always legal. */
312 break; 312 break;
313 313
314 case PT_ATTACH: 314 case PT_ATTACH:
315 /* 315 /*
316 * You can't attach to a process if: 316 * You can't attach to a process if:
317 * (1) it's the process that's doing the attaching, 317 * (1) it's the process that's doing the attaching,
318 */ 318 */
319 if (t->p_pid == p->p_pid) { 319 if (t->p_pid == p->p_pid) {
320 error = EINVAL; 320 error = EINVAL;
321 break; 321 break;
322 } 322 }
323 323
324 /* 324 /*
325 * (2) it's a system process 325 * (2) it's a system process
326 */ 326 */
327 if (t->p_flag & PK_SYSTEM) { 327 if (t->p_flag & PK_SYSTEM) {
328 error = EPERM; 328 error = EPERM;
329 break; 329 break;
330 } 330 }
331 331
332 /* 332 /*
333 * (3) it's already being traced, or 333 * (3) it's already being traced, or
334 */ 334 */
335 if (ISSET(t->p_slflag, PSL_TRACED)) { 335 if (ISSET(t->p_slflag, PSL_TRACED)) {
336 error = EBUSY; 336 error = EBUSY;
337 break; 337 break;
338 } 338 }
339 339
340 /* 340 /*
341 * (4) the tracer is chrooted, and its root directory is 341 * (4) the tracer is chrooted, and its root directory is
342 * not at or above the root directory of the tracee 342 * not at or above the root directory of the tracee
343 */ 343 */
344 mutex_exit(t->p_lock); /* XXXSMP */ 344 mutex_exit(t->p_lock); /* XXXSMP */
345 tmp = proc_isunder(t, l); 345 tmp = proc_isunder(t, l);
346 mutex_enter(t->p_lock); /* XXXSMP */ 346 mutex_enter(t->p_lock); /* XXXSMP */
347 if (!tmp) { 347 if (!tmp) {
348 error = EPERM; 348 error = EPERM;
349 break; 349 break;
350 } 350 }
351 break; 351 break;
352 352
353 case PT_READ_I: 353 case PT_READ_I:
354 case PT_READ_D: 354 case PT_READ_D:
355 case PT_WRITE_I: 355 case PT_WRITE_I:
356 case PT_WRITE_D: 356 case PT_WRITE_D:
357 case PT_IO: 357 case PT_IO:
358#ifdef PT_GETREGS 358#ifdef PT_GETREGS
359 case PT_GETREGS: 359 case PT_GETREGS:
360#endif 360#endif
361#ifdef PT_SETREGS 361#ifdef PT_SETREGS
362 case PT_SETREGS: 362 case PT_SETREGS:
363#endif 363#endif
364#ifdef PT_GETFPREGS 364#ifdef PT_GETFPREGS
365 case PT_GETFPREGS: 365 case PT_GETFPREGS:
366#endif 366#endif
367#ifdef PT_SETFPREGS 367#ifdef PT_SETFPREGS
368 case PT_SETFPREGS: 368 case PT_SETFPREGS:
369#endif 369#endif
370#ifdef __HAVE_PTRACE_MACHDEP 370#ifdef __HAVE_PTRACE_MACHDEP
371 PTRACE_MACHDEP_REQUEST_CASES 371 PTRACE_MACHDEP_REQUEST_CASES
372#endif 372#endif
373 /* 373 /*
374 * You can't read/write the memory or registers of a process 374 * You can't read/write the memory or registers of a process
375 * if the tracer is chrooted, and its root directory is not at 375 * if the tracer is chrooted, and its root directory is not at
376 * or above the root directory of the tracee. 376 * or above the root directory of the tracee.
377 */ 377 */
378 mutex_exit(t->p_lock); /* XXXSMP */ 378 mutex_exit(t->p_lock); /* XXXSMP */
379 tmp = proc_isunder(t, l); 379 tmp = proc_isunder(t, l);
380 mutex_enter(t->p_lock); /* XXXSMP */ 380 mutex_enter(t->p_lock); /* XXXSMP */
381 if (!tmp) { 381 if (!tmp) {
382 error = EPERM; 382 error = EPERM;
383 break; 383 break;
384 } 384 }
385 /*FALLTHROUGH*/ 385 /*FALLTHROUGH*/
386 386
387 case PT_CONTINUE: 387 case PT_CONTINUE:
388 case PT_KILL: 388 case PT_KILL:
389 case PT_DETACH: 389 case PT_DETACH:
390 case PT_LWPINFO: 390 case PT_LWPINFO:
391 case PT_SYSCALL: 391 case PT_SYSCALL:
392 case PT_SYSCALLEMU: 392 case PT_SYSCALLEMU:
393 case PT_DUMPCORE: 393 case PT_DUMPCORE:
394#ifdef PT_STEP 394#ifdef PT_STEP
395 case PT_STEP: 395 case PT_STEP:
396#endif 396#endif
397 case PT_SET_EVENT_MASK: 397 case PT_SET_EVENT_MASK:
398 case PT_GET_EVENT_MASK: 398 case PT_GET_EVENT_MASK:
399 case PT_GET_PROCESS_STATE: 399 case PT_GET_PROCESS_STATE:
400 /* 400 /*
401 * You can't do what you want to the process if: 401 * You can't do what you want to the process if:
402 * (1) It's not being traced at all, 402 * (1) It's not being traced at all,
403 */ 403 */
404 if (!ISSET(t->p_slflag, PSL_TRACED)) { 404 if (!ISSET(t->p_slflag, PSL_TRACED)) {
405 error = EPERM; 405 error = EPERM;
406 break; 406 break;
407 } 407 }
408 408
409 /* 409 /*
410 * (2) it's being traced by procfs (which has 410 * (2) it's being traced by procfs (which has
411 * different signal delivery semantics), 411 * different signal delivery semantics),
412 */ 412 */
413 if (ISSET(t->p_slflag, PSL_FSTRACE)) { 413 if (ISSET(t->p_slflag, PSL_FSTRACE)) {
414 DPRINTF(("file system traced\n")); 414 DPRINTF(("file system traced\n"));
415 error = EBUSY; 415 error = EBUSY;
416 break; 416 break;
417 } 417 }
418 418
419 /* 419 /*
420 * (3) it's not being traced by _you_, or 420 * (3) it's not being traced by _you_, or
421 */ 421 */
422 if (t->p_pptr != p) { 422 if (t->p_pptr != p) {
423 DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid, 423 DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
424 p->p_pid)); 424 p->p_pid));
425 error = EBUSY; 425 error = EBUSY;
426 break; 426 break;
427 } 427 }
428 428
429 /* 429 /*
430 * (4) it's not currently stopped. 430 * (4) it's not currently stopped.
431 */ 431 */
432 if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) { 432 if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
433 DPRINTF(("stat %d flag %d\n", t->p_stat, 433 DPRINTF(("stat %d flag %d\n", t->p_stat,
434 !t->p_waited)); 434 !t->p_waited));
435 error = EBUSY; 435 error = EBUSY;
436 break; 436 break;
437 } 437 }
438 break; 438 break;
439 439
440 default: /* It was not a legal request. */ 440 default: /* It was not a legal request. */
441 error = EINVAL; 441 error = EINVAL;
442 break; 442 break;
443 } 443 }
444 444
445 if (error == 0) { 445 if (error == 0) {
446 error = kauth_authorize_process(l->l_cred, 446 error = kauth_authorize_process(l->l_cred,
447 KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req), 447 KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
448 NULL, NULL); 448 NULL, NULL);
449 } 449 }
450 if (error == 0) { 450 if (error == 0) {
451 lt = lwp_find_first(t); 451 lt = lwp_find_first(t);
452 if (lt == NULL) 452 if (lt == NULL)
453 error = ESRCH; 453 error = ESRCH;
454 } 454 }
455 455
456 if (error != 0) { 456 if (error != 0) {
457 mutex_exit(proc_lock); 457 mutex_exit(proc_lock);
458 mutex_exit(t->p_lock); 458 mutex_exit(t->p_lock);
459 rw_exit(&t->p_reflock); 459 rw_exit(&t->p_reflock);
460 return error; 460 return error;
461 } 461 }
462 462
463 /* Do single-step fixup if needed. */ 463 /* Do single-step fixup if needed. */
464 FIX_SSTEP(t); 464 FIX_SSTEP(t);
465 KASSERT(lt != NULL); 465 KASSERT(lt != NULL);
466 lwp_addref(lt); 466 lwp_addref(lt);
467 467
468 /* 468 /*
469 * Which locks do we need held? XXX Ugly. 469 * Which locks do we need held? XXX Ugly.
470 */ 470 */
471 switch (req) { 471 switch (req) {
472#ifdef PT_STEP 472#ifdef PT_STEP
473 case PT_STEP: 473 case PT_STEP:
474#endif 474#endif
475 case PT_CONTINUE: 475 case PT_CONTINUE:
476 case PT_DETACH: 476 case PT_DETACH:
477 case PT_KILL: 477 case PT_KILL:
478 case PT_SYSCALL: 478 case PT_SYSCALL:
479 case PT_SYSCALLEMU: 479 case PT_SYSCALLEMU:
480 case PT_ATTACH: 480 case PT_ATTACH:
481 case PT_TRACE_ME: 481 case PT_TRACE_ME:
482 pheld = 1; 482 pheld = 1;
483 break; 483 break;
484 default: 484 default:
485 mutex_exit(proc_lock); 485 mutex_exit(proc_lock);
486 mutex_exit(t->p_lock); 486 mutex_exit(t->p_lock);
487 pheld = 0; 487 pheld = 0;
488 break; 488 break;
489 } 489 }
490 490
491 /* Now do the operation. */ 491 /* Now do the operation. */
492 write = 0; 492 write = 0;
493 *retval = 0; 493 *retval = 0;
494 tmp = 0; 494 tmp = 0;
495 resume_all = 1; 495 resume_all = 1;
496 496
497 switch (req) { 497 switch (req) {
498 case PT_TRACE_ME: 498 case PT_TRACE_ME:
499 /* Just set the trace flag. */ 499 /* Just set the trace flag. */
500 SET(t->p_slflag, PSL_TRACED); 500 SET(t->p_slflag, PSL_TRACED);
501 t->p_opptr = t->p_pptr; 501 t->p_opptr = t->p_pptr;
502 break; 502 break;
503 503
504 case PT_WRITE_I: /* XXX no separate I and D spaces */ 504 case PT_WRITE_I: /* XXX no separate I and D spaces */
505 case PT_WRITE_D: 505 case PT_WRITE_D:
506#if defined(__HAVE_RAS) 506#if defined(__HAVE_RAS)
507 /* 507 /*
508 * Can't write to a RAS 508 * Can't write to a RAS
509 */ 509 */
510 if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) { 510 if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
511 error = EACCES; 511 error = EACCES;
512 break; 512 break;
513 } 513 }
514#endif 514#endif
515 write = 1; 515 write = 1;
516 tmp = SCARG(uap, data); 516 tmp = SCARG(uap, data);
517 /* FALLTHROUGH */ 517 /* FALLTHROUGH */
518 518
519 case PT_READ_I: /* XXX no separate I and D spaces */ 519 case PT_READ_I: /* XXX no separate I and D spaces */
520 case PT_READ_D: 520 case PT_READ_D:
521 /* write = 0 done above. */ 521 /* write = 0 done above. */
522 iov.iov_base = (void *)&tmp; 522 iov.iov_base = (void *)&tmp;
523 iov.iov_len = sizeof(tmp); 523 iov.iov_len = sizeof(tmp);
524 uio.uio_iov = &iov; 524 uio.uio_iov = &iov;
525 uio.uio_iovcnt = 1; 525 uio.uio_iovcnt = 1;
526 uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr); 526 uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr);
527 uio.uio_resid = sizeof(tmp); 527 uio.uio_resid = sizeof(tmp);
528 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 528 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
529 UIO_SETUP_SYSSPACE(&uio); 529 UIO_SETUP_SYSSPACE(&uio);
530 530
531 error = process_domem(l, lt, &uio); 531 error = process_domem(l, lt, &uio);
532 if (!write) 532 if (!write)
533 *retval = tmp; 533 *retval = tmp;
534 break; 534 break;
535 535
536 case PT_IO: 536 case PT_IO:
537 error = copyin(SCARG(uap, addr), &piod, sizeof(piod)); 537 error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
538 if (error) 538 if (error)
539 break; 539 break;
540 540
541 iov.iov_base = piod.piod_addr; 541 iov.iov_base = piod.piod_addr;
542 iov.iov_len = piod.piod_len; 542 iov.iov_len = piod.piod_len;
543 uio.uio_iov = &iov; 543 uio.uio_iov = &iov;
544 uio.uio_iovcnt = 1; 544 uio.uio_iovcnt = 1;
545 uio.uio_offset = (off_t)(unsigned long)piod.piod_offs; 545 uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
546 uio.uio_resid = piod.piod_len; 546 uio.uio_resid = piod.piod_len;
547 547
548 switch (piod.piod_op) { 548 switch (piod.piod_op) {
549 case PIOD_READ_D: 549 case PIOD_READ_D:
550 case PIOD_READ_I: 550 case PIOD_READ_I:
551 uio.uio_rw = UIO_READ; 551 uio.uio_rw = UIO_READ;
552 break; 552 break;
553 case PIOD_WRITE_D: 553 case PIOD_WRITE_D:
554 case PIOD_WRITE_I: 554 case PIOD_WRITE_I:
555 /* 555 /*
556 * Can't write to a RAS 556 * Can't write to a RAS
557 */ 557 */
558 if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) { 558 if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
559 return (EACCES); 559 return EACCES;
560 } 560 }
561 uio.uio_rw = UIO_WRITE; 561 uio.uio_rw = UIO_WRITE;
562 break; 562 break;
563 case PIOD_READ_AUXV: 563 case PIOD_READ_AUXV:
564 req = PT_READ_D; 564 req = PT_READ_D;
565 uio.uio_rw = UIO_READ; 565 uio.uio_rw = UIO_READ;
566 tmp = t->p_execsw->es_arglen * PROC_PTRSZ(t); 566 tmp = t->p_execsw->es_arglen * PROC_PTRSZ(t);
567 if (uio.uio_offset > tmp) 567 if (uio.uio_offset > tmp)
568 return EIO; 568 return EIO;
569 if (uio.uio_resid > tmp - uio.uio_offset) 569 if (uio.uio_resid > tmp - uio.uio_offset)
570 uio.uio_resid = tmp - uio.uio_offset; 570 uio.uio_resid = tmp - uio.uio_offset;
571 piod.piod_len = iov.iov_len = uio.uio_resid; 571 piod.piod_len = iov.iov_len = uio.uio_resid;
572 error = process_auxv_offset(t, &uio); 572 error = process_auxv_offset(t, &uio);
573 if (error) 573 if (error)
574 return error; 574 return error;
575 break; 575 break;
576 default: 576 default:
577 error = EINVAL; 577 error = EINVAL;
578 break; 578 break;
579 } 579 }
580 if (error) 580 if (error)
581 break; 581 break;
582 error = proc_vmspace_getref(l->l_proc, &vm); 582 error = proc_vmspace_getref(l->l_proc, &vm);
583 if (error) 583 if (error)
584 break; 584 break;
585 uio.uio_vmspace = vm; 585 uio.uio_vmspace = vm;
586 586
587 error = process_domem(l, lt, &uio); 587 error = process_domem(l, lt, &uio);
588 piod.piod_len -= uio.uio_resid; 588 piod.piod_len -= uio.uio_resid;
589 (void) copyout(&piod, SCARG(uap, addr), sizeof(piod)); 589 (void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
590 uvmspace_free(vm); 590 uvmspace_free(vm);
591 break; 591 break;
592 592
593 case PT_DUMPCORE: 593 case PT_DUMPCORE:
594 if ((path = SCARG(uap, addr)) != NULL) { 594 if ((path = SCARG(uap, addr)) != NULL) {
595 char *dst; 595 char *dst;
596 len = SCARG(uap, data); 596 len = SCARG(uap, data);
597 597
598 if (len < 0 || len >= MAXPATHLEN) { 598 if (len < 0 || len >= MAXPATHLEN) {
599 error = EINVAL; 599 error = EINVAL;
600 break; 600 break;
601 } 601 }
602 dst = kmem_alloc(len + 1, KM_SLEEP); 602 dst = kmem_alloc(len + 1, KM_SLEEP);
603 if ((error = copyin(path, dst, len)) != 0) { 603 if ((error = copyin(path, dst, len)) != 0) {
604 kmem_free(dst, len + 1); 604 kmem_free(dst, len + 1);
605 break; 605 break;
606 } 606 }
607 path = dst; 607 path = dst;
608 path[len] = '\0'; 608 path[len] = '\0';
609 } 609 }
610 error = (*coredump_vec)(lt, path); 610 error = (*coredump_vec)(lt, path);
611 if (path) 611 if (path)
612 kmem_free(path, len + 1); 612 kmem_free(path, len + 1);
613 break; 613 break;
614 614
615#ifdef PT_STEP 615#ifdef PT_STEP
616 case PT_STEP: 616 case PT_STEP:
617 /* 617 /*
618 * From the 4.4BSD PRM: 618 * From the 4.4BSD PRM:
619 * "Execution continues as in request PT_CONTINUE; however 619 * "Execution continues as in request PT_CONTINUE; however
620 * as soon as possible after execution of at least one 620 * as soon as possible after execution of at least one
621 * instruction, execution stops again. [ ... ]" 621 * instruction, execution stops again. [ ... ]"
622 */ 622 */
623#endif 623#endif
624 case PT_CONTINUE: 624 case PT_CONTINUE:
625 case PT_SYSCALL: 625 case PT_SYSCALL:
626 case PT_DETACH: 626 case PT_DETACH:
627 if (req == PT_SYSCALL) { 627 if (req == PT_SYSCALL) {
628 if (!ISSET(t->p_slflag, PSL_SYSCALL)) { 628 if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
629 SET(t->p_slflag, PSL_SYSCALL); 629 SET(t->p_slflag, PSL_SYSCALL);
630#ifdef __HAVE_SYSCALL_INTERN 630#ifdef __HAVE_SYSCALL_INTERN
631 (*t->p_emul->e_syscall_intern)(t); 631 (*t->p_emul->e_syscall_intern)(t);
632#endif 632#endif
633 } 633 }
634 } else { 634 } else {
635 if (ISSET(t->p_slflag, PSL_SYSCALL)) { 635 if (ISSET(t->p_slflag, PSL_SYSCALL)) {
636 CLR(t->p_slflag, PSL_SYSCALL); 636 CLR(t->p_slflag, PSL_SYSCALL);
637#ifdef __HAVE_SYSCALL_INTERN 637#ifdef __HAVE_SYSCALL_INTERN
638 (*t->p_emul->e_syscall_intern)(t); 638 (*t->p_emul->e_syscall_intern)(t);
639#endif 639#endif
640 } 640 }
641 } 641 }
642 t->p_trace_enabled = trace_is_enabled(t); 642 t->p_trace_enabled = trace_is_enabled(t);
643 643
644 /* 644 /*
645 * Pick up the LWPID, if supplied. There are two cases: 645 * Pick up the LWPID, if supplied. There are two cases:
646 * data < 0 : step or continue single thread, lwp = -data 646 * data < 0 : step or continue single thread, lwp = -data
647 * data > 0 in PT_STEP : step this thread, continue others 647 * data > 0 in PT_STEP : step this thread, continue others
648 * For operations other than PT_STEP, data > 0 means 648 * For operations other than PT_STEP, data > 0 means
649 * data is the signo to deliver to the process. 649 * data is the signo to deliver to the process.
650 */ 650 */
651 tmp = SCARG(uap, data); 651 tmp = SCARG(uap, data);
652 if (tmp >= 0) { 652 if (tmp >= 0) {
653#ifdef PT_STEP 653#ifdef PT_STEP
654 if (req == PT_STEP) 654 if (req == PT_STEP)
655 signo = 0; 655 signo = 0;
656 else 656 else
657#endif 657#endif
658 { 658 {
659 signo = tmp; 659 signo = tmp;
660 tmp = 0; /* don't search for LWP */ 660 tmp = 0; /* don't search for LWP */
661 } 661 }
662 } else 662 } else
663 tmp = -tmp; 663 tmp = -tmp;
664 664
665 if (tmp > 0) { 665 if (tmp > 0) {
666 if (req == PT_DETACH) { 666 if (req == PT_DETACH) {
667 error = EINVAL; 667 error = EINVAL;
668 break; 668 break;
669 } 669 }
670 lwp_delref2 (lt); 670 lwp_delref2 (lt);
671 lt = lwp_find(t, tmp); 671 lt = lwp_find(t, tmp);
672 if (lt == NULL) { 672 if (lt == NULL) {
673 error = ESRCH; 673 error = ESRCH;
674 break; 674 break;
675 } 675 }
676 lwp_addref(lt); 676 lwp_addref(lt);
677 resume_all = 0; 677 resume_all = 0;
678 signo = 0; 678 signo = 0;
679 } 679 }
680 680
681 /* 681 /*
682 * From the 4.4BSD PRM: 682 * From the 4.4BSD PRM:
683 * "The data argument is taken as a signal number and the 683 * "The data argument is taken as a signal number and the
684 * child's execution continues at location addr as if it 684 * child's execution continues at location addr as if it
685 * incurred that signal. Normally the signal number will 685 * incurred that signal. Normally the signal number will
686 * be either 0 to indicate that the signal that caused the 686 * be either 0 to indicate that the signal that caused the
687 * stop should be ignored, or that value fetched out of 687 * stop should be ignored, or that value fetched out of
688 * the process's image indicating which signal caused 688 * the process's image indicating which signal caused
689 * the stop. If addr is (int *)1 then execution continues 689 * the stop. If addr is (int *)1 then execution continues
690 * from where it stopped." 690 * from where it stopped."
691 */ 691 */
692 692
693 /* Check that the data is a valid signal number or zero. */ 693 /* Check that the data is a valid signal number or zero. */
694 if (signo < 0 || signo >= NSIG) { 694 if (signo < 0 || signo >= NSIG) {
695 error = EINVAL; 695 error = EINVAL;
696 break; 696 break;
697 } 697 }
698 698
699 /* If the address parameter is not (int *)1, set the pc. */ 699 /* If the address parameter is not (int *)1, set the pc. */
700 if ((int *)SCARG(uap, addr) != (int *)1) { 700 if ((int *)SCARG(uap, addr) != (int *)1) {
701 error = process_set_pc(lt, SCARG(uap, addr)); 701 error = process_set_pc(lt, SCARG(uap, addr));
702 if (error != 0) 702 if (error != 0)
703 break; 703 break;
704 } 704 }
705#ifdef PT_STEP 705#ifdef PT_STEP
706 /* 706 /*
707 * Arrange for a single-step, if that's requested and possible. 707 * Arrange for a single-step, if that's requested and possible.
708 * More precisely, set the single step status as requested for 708 * More precisely, set the single step status as requested for
709 * the requested thread, and clear it for other threads. 709 * the requested thread, and clear it for other threads.
710 */ 710 */
711 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) { 711 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
712 if (lt != lt2) { 712 if (lt != lt2) {
713 lwp_lock(lt2); 713 lwp_lock(lt2);
714 process_sstep(lt2, 0); 714 process_sstep(lt2, 0);
715 lwp_unlock(lt2); 715 lwp_unlock(lt2);
716 } 716 }
717 } 717 }
718 error = process_sstep(lt, req == PT_STEP); 718 error = process_sstep(lt, req == PT_STEP);
719 if (error) 719 if (error)
720 break; 720 break;
721#endif 721#endif
722 if (req == PT_DETACH) { 722 if (req == PT_DETACH) {
723 CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL); 723 CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
724 724
725 /* give process back to original parent or init */ 725 /* give process back to original parent or init */
726 if (t->p_opptr != t->p_pptr) { 726 if (t->p_opptr != t->p_pptr) {
727 struct proc *pp = t->p_opptr; 727 struct proc *pp = t->p_opptr;
728 proc_reparent(t, pp ? pp : initproc); 728 proc_reparent(t, pp ? pp : initproc);
729 } 729 }
730 730
731 /* not being traced any more */ 731 /* not being traced any more */
732 t->p_opptr = NULL; 732 t->p_opptr = NULL;
733 } 733 }
734 sendsig: 734 sendsig:
735 t->p_fpid = 0; 735 t->p_fpid = 0;
736 /* Finally, deliver the requested signal (or none). */ 736 /* Finally, deliver the requested signal (or none). */
737 if (t->p_stat == SSTOP) { 737 if (t->p_stat == SSTOP) {
738 /* 738 /*
739 * Unstop the process. If it needs to take a 739 * Unstop the process. If it needs to take a
740 * signal, make all efforts to ensure that at 740 * signal, make all efforts to ensure that at
741 * an LWP runs to see it. 741 * an LWP runs to see it.
742 */ 742 */
743 t->p_xsig = signo; 743 t->p_xsig = signo;
744 if (resume_all) 744 if (resume_all)
745 proc_unstop(t); 745 proc_unstop(t);
746 else 746 else
747 lwp_unstop(lt); 747 lwp_unstop(lt);
748 } else if (signo != 0) { 748 } else if (signo != 0) {
749 KSI_INIT_EMPTY(&ksi); 749 KSI_INIT_EMPTY(&ksi);
750 ksi.ksi_signo = signo; 750 ksi.ksi_signo = signo;
751 kpsignal2(t, &ksi); 751 kpsignal2(t, &ksi);
752 } 752 }
753 break; 753 break;
754 754
755 case PT_SYSCALLEMU: 755 case PT_SYSCALLEMU:
756 if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) { 756 if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
757 error = EINVAL; 757 error = EINVAL;
758 break; 758 break;
759 } 759 }
760 SET(t->p_slflag, PSL_SYSCALLEMU); 760 SET(t->p_slflag, PSL_SYSCALLEMU);
761 break; 761 break;
762 762
763 case PT_KILL: 763 case PT_KILL:
764 /* just send the process a KILL signal. */ 764 /* just send the process a KILL signal. */
765 signo = SIGKILL; 765 signo = SIGKILL;
766 goto sendsig; /* in PT_CONTINUE, above. */ 766 goto sendsig; /* in PT_CONTINUE, above. */
767 767
768 case PT_ATTACH: 768 case PT_ATTACH:
769 /* 769 /*
770 * Go ahead and set the trace flag. 770 * Go ahead and set the trace flag.
771 * Save the old parent (it's reset in 771 * Save the old parent (it's reset in
772 * _DETACH, and also in kern_exit.c:wait4() 772 * _DETACH, and also in kern_exit.c:wait4()
773 * Reparent the process so that the tracing 773 * Reparent the process so that the tracing
774 * proc gets to see all the action. 774 * proc gets to see all the action.
775 * Stop the target. 775 * Stop the target.
776 */ 776 */
777 t->p_opptr = t->p_pptr; 777 t->p_opptr = t->p_pptr;
778 if (t->p_pptr != p) { 778 if (t->p_pptr != p) {
779 struct proc *parent = t->p_pptr; 779 struct proc *parent = t->p_pptr;
780 780
781 if (parent->p_lock < t->p_lock) { 781 if (parent->p_lock < t->p_lock) {
782 if (!mutex_tryenter(parent->p_lock)) { 782 if (!mutex_tryenter(parent->p_lock)) {
783 mutex_exit(t->p_lock); 783 mutex_exit(t->p_lock);
784 mutex_enter(parent->p_lock); 784 mutex_enter(parent->p_lock);
785 mutex_enter(t->p_lock); 785 mutex_enter(t->p_lock);
786 } 786 }
787 } else if (parent->p_lock > t->p_lock) { 787 } else if (parent->p_lock > t->p_lock) {
788 mutex_enter(parent->p_lock); 788 mutex_enter(parent->p_lock);
789 } 789 }
790 parent->p_slflag |= PSL_CHTRACED; 790 parent->p_slflag |= PSL_CHTRACED;
791 proc_reparent(t, p); 791 proc_reparent(t, p);
792 if (parent->p_lock != t->p_lock) 792 if (parent->p_lock != t->p_lock)
793 mutex_exit(parent->p_lock); 793 mutex_exit(parent->p_lock);
794 } 794 }
795 SET(t->p_slflag, PSL_TRACED); 795 SET(t->p_slflag, PSL_TRACED);
796 signo = SIGSTOP; 796 signo = SIGSTOP;
797 goto sendsig; 797 goto sendsig;
798 798
799 case PT_GET_EVENT_MASK: 799 case PT_GET_EVENT_MASK:
800 if (SCARG(uap, data) != sizeof(pe)) { 800 if (SCARG(uap, data) != sizeof(pe)) {
801 DPRINTF(("ptrace(%d): %d != %zu\n", req, 801 DPRINTF(("ptrace(%d): %d != %zu\n", req,
802 SCARG(uap, data), sizeof(pe))); 802 SCARG(uap, data), sizeof(pe)));
803 error = EINVAL; 803 error = EINVAL;
804 break; 804 break;
805 } 805 }
806 memset(&pe, 0, sizeof(pe)); 806 memset(&pe, 0, sizeof(pe));
807 pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ? 807 pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
808 PTRACE_FORK : 0; 808 PTRACE_FORK : 0;
809 error = copyout(&pe, SCARG(uap, addr), sizeof(pe)); 809 error = copyout(&pe, SCARG(uap, addr), sizeof(pe));
810 break; 810 break;
811 811
812 case PT_SET_EVENT_MASK: 812 case PT_SET_EVENT_MASK:
813 if (SCARG(uap, data) != sizeof(pe)) { 813 if (SCARG(uap, data) != sizeof(pe)) {
814 DPRINTF(("ptrace(%d): %d != %zu\n", req, 814 DPRINTF(("ptrace(%d): %d != %zu\n", req,
815 SCARG(uap, data), sizeof(pe))); 815 SCARG(uap, data), sizeof(pe)));
816 error = EINVAL; 816 error = EINVAL;
817 break; 817 break;
818 } 818 }
819 if ((error = copyin(SCARG(uap, addr), &pe, sizeof(pe))) != 0) 819 if ((error = copyin(SCARG(uap, addr), &pe, sizeof(pe))) != 0)
820 return error; 820 return error;
821 if (pe.pe_set_event & PTRACE_FORK) 821 if (pe.pe_set_event & PTRACE_FORK)
822 SET(t->p_slflag, PSL_TRACEFORK); 822 SET(t->p_slflag, PSL_TRACEFORK);
823 else 823 else
824 CLR(t->p_slflag, PSL_TRACEFORK); 824 CLR(t->p_slflag, PSL_TRACEFORK);
825 break; 825 break;
826 826
827 case PT_GET_PROCESS_STATE: 827 case PT_GET_PROCESS_STATE:
828 if (SCARG(uap, data) != sizeof(ps)) { 828 if (SCARG(uap, data) != sizeof(ps)) {
829 DPRINTF(("ptrace(%d): %d != %zu\n", req, 829 DPRINTF(("ptrace(%d): %d != %zu\n", req,
830 SCARG(uap, data), sizeof(ps))); 830 SCARG(uap, data), sizeof(ps)));
831 error = EINVAL; 831 error = EINVAL;
832 break; 832 break;
833 } 833 }
834 memset(&ps, 0, sizeof(ps)); 834 memset(&ps, 0, sizeof(ps));
835 if (t->p_fpid) { 835 if (t->p_fpid) {
836 ps.pe_report_event = PTRACE_FORK; 836 ps.pe_report_event = PTRACE_FORK;
837 ps.pe_other_pid = t->p_fpid; 837 ps.pe_other_pid = t->p_fpid;
838 } 838 }
839 error = copyout(&ps, SCARG(uap, addr), sizeof(ps)); 839 error = copyout(&ps, SCARG(uap, addr), sizeof(ps));
840 break; 840 break;
841 841
842 case PT_LWPINFO: 842 case PT_LWPINFO:
843 if (SCARG(uap, data) != sizeof(pl)) { 843 if (SCARG(uap, data) != sizeof(pl)) {
844 DPRINTF(("ptrace(%d): %d != %zu\n", req, 844 DPRINTF(("ptrace(%d): %d != %zu\n", req,
845 SCARG(uap, data), sizeof(pl))); 845 SCARG(uap, data), sizeof(pl)));
846 error = EINVAL; 846 error = EINVAL;
847 break; 847 break;
848 } 848 }
849 error = copyin(SCARG(uap, addr), &pl, sizeof(pl)); 849 error = copyin(SCARG(uap, addr), &pl, sizeof(pl));
850 if (error) 850 if (error)
851 break; 851 break;
852 tmp = pl.pl_lwpid; 852 tmp = pl.pl_lwpid;
853 lwp_delref(lt); 853 lwp_delref(lt);
854 mutex_enter(t->p_lock); 854 mutex_enter(t->p_lock);
855 if (tmp == 0) 855 if (tmp == 0)
856 lt = lwp_find_first(t); 856 lt = lwp_find_first(t);
857 else { 857 else {
858 lt = lwp_find(t, tmp); 858 lt = lwp_find(t, tmp);
859 if (lt == NULL) { 859 if (lt == NULL) {
860 mutex_exit(t->p_lock); 860 mutex_exit(t->p_lock);
861 error = ESRCH; 861 error = ESRCH;
862 break; 862 break;
863 } 863 }
864 lt = LIST_NEXT(lt, l_sibling); 864 lt = LIST_NEXT(lt, l_sibling);
865 } 865 }
866 while (lt != NULL && !lwp_alive(lt)) 866 while (lt != NULL && !lwp_alive(lt))
867 lt = LIST_NEXT(lt, l_sibling); 867 lt = LIST_NEXT(lt, l_sibling);
868 pl.pl_lwpid = 0; 868 pl.pl_lwpid = 0;
869 pl.pl_event = 0; 869 pl.pl_event = 0;
870 if (lt) { 870 if (lt) {
871 lwp_addref(lt); 871 lwp_addref(lt);
872 pl.pl_lwpid = lt->l_lid; 872 pl.pl_lwpid = lt->l_lid;
873 if (lt->l_lid == t->p_sigctx.ps_lwp) 873 if (lt->l_lid == t->p_sigctx.ps_lwp)
874 pl.pl_event = PL_EVENT_SIGNAL; 874 pl.pl_event = PL_EVENT_SIGNAL;
875 } 875 }
876 mutex_exit(t->p_lock); 876 mutex_exit(t->p_lock);
877 877
878 error = copyout(&pl, SCARG(uap, addr), sizeof(pl)); 878 error = copyout(&pl, SCARG(uap, addr), sizeof(pl));
879 break; 879 break;
880 880
881#ifdef PT_SETREGS 881#ifdef PT_SETREGS
882 case PT_SETREGS: 882 case PT_SETREGS:
883 write = 1; 883 write = 1;
884#endif 884#endif
885#ifdef PT_GETREGS 885#ifdef PT_GETREGS
886 case PT_GETREGS: 886 case PT_GETREGS:
887 /* write = 0 done above. */ 887 /* write = 0 done above. */
888#endif 888#endif
889#if defined(PT_SETREGS) || defined(PT_GETREGS) 889#if defined(PT_SETREGS) || defined(PT_GETREGS)
890 tmp = SCARG(uap, data); 890 tmp = SCARG(uap, data);
891 if (tmp != 0 && t->p_nlwps > 1) { 891 if (tmp != 0 && t->p_nlwps > 1) {
892 lwp_delref(lt); 892 lwp_delref(lt);
893 mutex_enter(t->p_lock); 893 mutex_enter(t->p_lock);
894 lt = lwp_find(t, tmp); 894 lt = lwp_find(t, tmp);
895 if (lt == NULL) { 895 if (lt == NULL) {
896 mutex_exit(t->p_lock); 896 mutex_exit(t->p_lock);
897 error = ESRCH; 897 error = ESRCH;
898 break; 898 break;
899 } 899 }
900 lwp_addref(lt); 900 lwp_addref(lt);
901 mutex_exit(t->p_lock); 901 mutex_exit(t->p_lock);
902 } 902 }
903 if (!process_validregs(lt)) 903 if (!process_validregs(lt))
904 error = EINVAL; 904 error = EINVAL;
905 else { 905 else {
906 error = proc_vmspace_getref(l->l_proc, &vm); 906 error = proc_vmspace_getref(l->l_proc, &vm);
907 if (error) 907 if (error)
908 break; 908 break;
909 iov.iov_base = SCARG(uap, addr); 909 iov.iov_base = SCARG(uap, addr);
910 iov.iov_len = sizeof(struct reg); 910 iov.iov_len = sizeof(struct reg);
911 uio.uio_iov = &iov; 911 uio.uio_iov = &iov;
912 uio.uio_iovcnt = 1; 912 uio.uio_iovcnt = 1;
913 uio.uio_offset = 0; 913 uio.uio_offset = 0;
914 uio.uio_resid = sizeof(struct reg); 914 uio.uio_resid = sizeof(struct reg);
915 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 915 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
916 uio.uio_vmspace = vm; 916 uio.uio_vmspace = vm;
917 917
918 error = process_doregs(l, lt, &uio); 918 error = process_doregs(l, lt, &uio);
919 uvmspace_free(vm); 919 uvmspace_free(vm);
920 } 920 }
921 break; 921 break;
922#endif 922#endif
923 923
924#ifdef PT_SETFPREGS 924#ifdef PT_SETFPREGS
925 case PT_SETFPREGS: 925 case PT_SETFPREGS:
926 write = 1; 926 write = 1;
927#endif 927#endif
928#ifdef PT_GETFPREGS 928#ifdef PT_GETFPREGS
929 case PT_GETFPREGS: 929 case PT_GETFPREGS:
930 /* write = 0 done above. */ 930 /* write = 0 done above. */
931#endif 931#endif
932#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 932#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
933 tmp = SCARG(uap, data); 933 tmp = SCARG(uap, data);
934 if (tmp != 0 && t->p_nlwps > 1) { 934 if (tmp != 0 && t->p_nlwps > 1) {
935 lwp_delref(lt); 935 lwp_delref(lt);
936 mutex_enter(t->p_lock); 936 mutex_enter(t->p_lock);
937 lt = lwp_find(t, tmp); 937 lt = lwp_find(t, tmp);
938 if (lt == NULL) { 938 if (lt == NULL) {
939 mutex_exit(t->p_lock); 939 mutex_exit(t->p_lock);
940 error = ESRCH; 940 error = ESRCH;
941 break; 941 break;
942 } 942 }
943 lwp_addref(lt); 943 lwp_addref(lt);
944 mutex_exit(t->p_lock); 944 mutex_exit(t->p_lock);
945 } 945 }
946 if (!process_validfpregs(lt)) 946 if (!process_validfpregs(lt))
947 error = EINVAL; 947 error = EINVAL;
948 else { 948 else {
949 error = proc_vmspace_getref(l->l_proc, &vm); 949 error = proc_vmspace_getref(l->l_proc, &vm);
950 if (error) 950 if (error)
951 break; 951 break;
952 iov.iov_base = SCARG(uap, addr); 952 iov.iov_base = SCARG(uap, addr);
953 iov.iov_len = sizeof(struct fpreg); 953 iov.iov_len = sizeof(struct fpreg);
954 uio.uio_iov = &iov; 954 uio.uio_iov = &iov;
955 uio.uio_iovcnt = 1; 955 uio.uio_iovcnt = 1;
956 uio.uio_offset = 0; 956 uio.uio_offset = 0;
957 uio.uio_resid = sizeof(struct fpreg); 957 uio.uio_resid = sizeof(struct fpreg);
958 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 958 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
959 uio.uio_vmspace = vm; 959 uio.uio_vmspace = vm;
960 960
961 error = process_dofpregs(l, lt, &uio); 961 error = process_dofpregs(l, lt, &uio);
962 uvmspace_free(vm); 962 uvmspace_free(vm);
963 } 963 }
964 break; 964 break;
965#endif 965#endif
966 966
967#ifdef __HAVE_PTRACE_MACHDEP 967#ifdef __HAVE_PTRACE_MACHDEP
968 PTRACE_MACHDEP_REQUEST_CASES 968 PTRACE_MACHDEP_REQUEST_CASES
969 error = ptrace_machdep_dorequest(l, lt, 969 error = ptrace_machdep_dorequest(l, lt,
970 req, SCARG(uap, addr), SCARG(uap, data)); 970 req, SCARG(uap, addr), SCARG(uap, data));
971 break; 971 break;
972#endif 972#endif
973 } 973 }
974 974
975 if (pheld) { 975 if (pheld) {
976 mutex_exit(t->p_lock); 976 mutex_exit(t->p_lock);
977 mutex_exit(proc_lock); 977 mutex_exit(proc_lock);
978 } 978 }
979 if (lt != NULL) 979 if (lt != NULL)
980 lwp_delref(lt); 980 lwp_delref(lt);
981 rw_exit(&t->p_reflock); 981 rw_exit(&t->p_reflock);
982 982
983 return error; 983 return error;
984} 984}
985 985
986int 986int
987process_doregs(struct lwp *curl /*tracer*/, 987process_doregs(struct lwp *curl /*tracer*/,
988 struct lwp *l /*traced*/, 988 struct lwp *l /*traced*/,
989 struct uio *uio) 989 struct uio *uio)
990{ 990{
991#if defined(PT_GETREGS) || defined(PT_SETREGS) 991#if defined(PT_GETREGS) || defined(PT_SETREGS)
992 int error; 992 int error;
993 struct reg r; 993 struct reg r;
994 char *kv; 994 char *kv;
995 int kl; 995 int kl;
996 996
997 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r)) 997 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
998 return EINVAL; 998 return EINVAL;
999 999
1000 kl = sizeof(r); 1000 kl = sizeof(r);
1001 kv = (char *)&r; 1001 kv = (char *)&r;
1002 1002
1003 kv += uio->uio_offset; 1003 kv += uio->uio_offset;
1004 kl -= uio->uio_offset; 1004 kl -= uio->uio_offset;
1005 if ((size_t)kl > uio->uio_resid) 1005 if ((size_t)kl > uio->uio_resid)
1006 kl = uio->uio_resid; 1006 kl = uio->uio_resid;
1007 1007
1008 error = process_read_regs(l, &r); 1008 error = process_read_regs(l, &r);
1009 if (error == 0) 1009 if (error == 0)
1010 error = uiomove(kv, kl, uio); 1010 error = uiomove(kv, kl, uio);
1011 if (error == 0 && uio->uio_rw == UIO_WRITE) { 1011 if (error == 0 && uio->uio_rw == UIO_WRITE) {
1012 if (l->l_stat != LSSTOP) 1012 if (l->l_stat != LSSTOP)
1013 error = EBUSY; 1013 error = EBUSY;
1014 else 1014 else
1015 error = process_write_regs(l, &r); 1015 error = process_write_regs(l, &r);
1016 } 1016 }
1017 1017
1018 uio->uio_offset = 0; 1018 uio->uio_offset = 0;
1019 return (error); 1019 return error;
1020#else 1020#else
1021 return (EINVAL); 1021 return EINVAL;
1022#endif 1022#endif
1023} 1023}
1024 1024
1025int 1025int
1026process_validregs(struct lwp *l) 1026process_validregs(struct lwp *l)
1027{ 1027{
1028 1028
1029#if defined(PT_SETREGS) || defined(PT_GETREGS) 1029#if defined(PT_SETREGS) || defined(PT_GETREGS)
1030 return ((l->l_flag & LW_SYSTEM) == 0); 1030 return (l->l_flag & LW_SYSTEM) == 0;
1031#else 1031#else
1032 return (0); 1032 return 0;
1033#endif 1033#endif
1034} 1034}
1035 1035
1036int 1036int
1037process_dofpregs(struct lwp *curl /*tracer*/, 1037process_dofpregs(struct lwp *curl /*tracer*/,
1038 struct lwp *l /*traced*/, 1038 struct lwp *l /*traced*/,
1039 struct uio *uio) 1039 struct uio *uio)
1040{ 1040{
1041#if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 1041#if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
1042 int error; 1042 int error;
1043 struct fpreg r; 1043 struct fpreg r;
1044 char *kv; 1044 char *kv;
1045 size_t kl; 1045 size_t kl;
1046 1046
1047 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r)) 1047 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
1048 return EINVAL; 1048 return EINVAL;
1049 1049
1050 kl = sizeof(r); 1050 kl = sizeof(r);
1051 kv = (char *)&r; 1051 kv = (char *)&r;
1052 1052
1053 kv += uio->uio_offset; 1053 kv += uio->uio_offset;
1054 kl -= uio->uio_offset; 1054 kl -= uio->uio_offset;
1055 if (kl > uio->uio_resid) 1055 if (kl > uio->uio_resid)
1056 kl = uio->uio_resid; 1056 kl = uio->uio_resid;
1057 1057
1058 error = process_read_fpregs(l, &r, &kl); 1058 error = process_read_fpregs(l, &r, &kl);
1059 if (error == 0) 1059 if (error == 0)
1060 error = uiomove(kv, kl, uio); 1060 error = uiomove(kv, kl, uio);
1061 if (error == 0 && uio->uio_rw == UIO_WRITE) { 1061 if (error == 0 && uio->uio_rw == UIO_WRITE) {
1062 if (l->l_stat != LSSTOP) 1062 if (l->l_stat != LSSTOP)
1063 error = EBUSY; 1063 error = EBUSY;
1064 else 1064 else
1065 error = process_write_fpregs(l, &r, kl); 1065 error = process_write_fpregs(l, &r, kl);
1066 } 1066 }
1067 uio->uio_offset = 0; 1067 uio->uio_offset = 0;
1068 return (error); 1068 return error;
1069#else 1069#else
1070 return (EINVAL); 1070 return EINVAL;
1071#endif 1071#endif
1072} 1072}
1073 1073
1074int 1074int
1075process_validfpregs(struct lwp *l) 1075process_validfpregs(struct lwp *l)
1076{ 1076{
1077 1077
1078#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 1078#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
1079 return ((l->l_flag & LW_SYSTEM) == 0); 1079 return (l->l_flag & LW_SYSTEM) == 0;
1080#else 1080#else
1081 return (0); 1081 return 0;
1082#endif 1082#endif
1083} 1083}
1084#endif /* PTRACE */ 1084#endif /* PTRACE */
1085 1085
1086#if defined(KTRACE) || defined(PTRACE) 1086#if defined(KTRACE) || defined(PTRACE)
1087int 1087int
1088process_domem(struct lwp *curl /*tracer*/, 1088process_domem(struct lwp *curl /*tracer*/,
1089 struct lwp *l /*traced*/, 1089 struct lwp *l /*traced*/,
1090 struct uio *uio) 1090 struct uio *uio)
1091{ 1091{
1092 struct proc *p = l->l_proc; /* traced */ 1092 struct proc *p = l->l_proc; /* traced */
1093 struct vmspace *vm; 1093 struct vmspace *vm;
1094 int error; 1094 int error;
1095 1095
1096 size_t len; 1096 size_t len;
1097#ifdef PMAP_NEED_PROCWR 1097#ifdef PMAP_NEED_PROCWR
1098 vaddr_t addr; 1098 vaddr_t addr;
1099#endif 1099#endif
1100 1100
1101 error = 0; 1101 error = 0;
1102 len = uio->uio_resid; 1102 len = uio->uio_resid;
1103 1103
1104 if (len == 0) 1104 if (len == 0)
1105 return (0); 1105 return 0;
1106 1106
1107#ifdef PMAP_NEED_PROCWR 1107#ifdef PMAP_NEED_PROCWR
1108 addr = uio->uio_offset; 1108 addr = uio->uio_offset;
1109#endif 1109#endif
1110 1110
1111 vm = p->p_vmspace; 1111 vm = p->p_vmspace;
1112 1112
1113 mutex_enter(&vm->vm_map.misc_lock); 1113 mutex_enter(&vm->vm_map.misc_lock);
1114 if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1) 1114 if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1)
1115 error = EFAULT; 1115 error = EFAULT;
1116 if (error == 0) 1116 if (error == 0)
1117 p->p_vmspace->vm_refcnt++; /* XXX */ 1117 p->p_vmspace->vm_refcnt++; /* XXX */
1118 mutex_exit(&vm->vm_map.misc_lock); 1118 mutex_exit(&vm->vm_map.misc_lock);
1119 if (error != 0) 1119 if (error != 0)
1120 return (error); 1120 return error;
1121 error = uvm_io(&vm->vm_map, uio, pax_mprotect_prot(l)); 1121 error = uvm_io(&vm->vm_map, uio, pax_mprotect_prot(l));
1122 uvmspace_free(vm); 1122 uvmspace_free(vm);
1123 1123
1124#ifdef PMAP_NEED_PROCWR 1124#ifdef PMAP_NEED_PROCWR
1125 if (error == 0 && uio->uio_rw == UIO_WRITE) 1125 if (error == 0 && uio->uio_rw == UIO_WRITE)
1126 pmap_procwr(p, addr, len); 1126 pmap_procwr(p, addr, len);
1127#endif 1127#endif
1128 return (error); 1128 return error;
1129} 1129}
1130#endif /* KTRACE || PTRACE */ 1130#endif /* KTRACE || PTRACE */
1131 1131
1132#if defined(KTRACE) || defined(PTRACE) 1132#if defined(KTRACE) || defined(PTRACE)
1133void 1133void
1134process_stoptrace(void) 1134process_stoptrace(void)
1135{ 1135{
1136 struct lwp *l = curlwp; 1136 struct lwp *l = curlwp;
1137 struct proc *p = l->l_proc, *pp; 1137 struct proc *p = l->l_proc, *pp;
1138 1138
1139 mutex_enter(proc_lock); 1139 mutex_enter(proc_lock);
1140 mutex_enter(p->p_lock); 1140 mutex_enter(p->p_lock);
1141 pp = p->p_pptr; 1141 pp = p->p_pptr;
1142 if (pp->p_pid == 1) { 1142 if (pp->p_pid == 1) {
1143 CLR(p->p_slflag, PSL_SYSCALL); /* XXXSMP */ 1143 CLR(p->p_slflag, PSL_SYSCALL); /* XXXSMP */
1144 mutex_exit(p->p_lock); 1144 mutex_exit(p->p_lock);
1145 mutex_exit(proc_lock); 1145 mutex_exit(proc_lock);
1146 return; 1146 return;
1147 } 1147 }
1148 1148
1149 p->p_xsig = SIGTRAP; 1149 p->p_xsig = SIGTRAP;
1150 proc_stop(p, 1, SIGSTOP); 1150 proc_stop(p, 1, SIGSTOP);
1151 mutex_exit(proc_lock); 1151 mutex_exit(proc_lock);
1152 1152
1153 if (sigispending(l, 0)) { 1153 if (sigispending(l, 0)) {
1154 lwp_lock(l); 1154 lwp_lock(l);
1155 l->l_flag |= LW_PENDSIG; 1155 l->l_flag |= LW_PENDSIG;
1156 lwp_unlock(l); 1156 lwp_unlock(l);
1157 } 1157 }
1158 mutex_exit(p->p_lock); 1158 mutex_exit(p->p_lock);
1159} 1159}
1160#endif /* KTRACE || PTRACE */ 1160#endif /* KTRACE || PTRACE */
1161 1161
1162#ifdef PTRACE 1162#ifdef PTRACE
1163static int 1163static int
1164process_auxv_offset(struct proc *p, struct uio *uio) 1164process_auxv_offset(struct proc *p, struct uio *uio)
1165{ 1165{
1166 struct ps_strings pss; 1166 struct ps_strings pss;
1167 int error; 1167 int error;
1168 off_t off = (off_t)p->p_psstrp; 1168 off_t off = (off_t)p->p_psstrp;
1169 1169
1170 if ((error = copyin_psstrings(p, &pss)) != 0) 1170 if ((error = copyin_psstrings(p, &pss)) != 0)
1171 return error; 1171 return error;
1172 1172
1173 if (pss.ps_envstr == NULL) 1173 if (pss.ps_envstr == NULL)
1174 return EIO; 1174 return EIO;
1175 1175
1176 uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1); 1176 uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
1177#ifdef __MACHINE_STACK_GROWS_UP 1177#ifdef __MACHINE_STACK_GROWS_UP
1178 if (uio->uio_offset < off) 1178 if (uio->uio_offset < off)
1179 return EIO; 1179 return EIO;
1180#else 1180#else
1181 if (uio->uio_offset > off) 1181 if (uio->uio_offset > off)
1182 return EIO; 1182 return EIO;
1183 if ((uio->uio_offset + uio->uio_resid) > off) 1183 if ((uio->uio_offset + uio->uio_resid) > off)
1184 uio->uio_resid = off - uio->uio_offset; 1184 uio->uio_resid = off - uio->uio_offset;
1185#endif 1185#endif
1186 return 0; 1186 return 0;
1187} 1187}
1188#endif 1188#endif