]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_process.c
xnu-344.34.tar.gz
[apple/xnu.git] / bsd / kern / mach_process.c
CommitLineData
1c79356b 1/*
9bccf70c 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
de355530
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
de355530
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
de355530
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*-
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
61 */
1c79356b
A
62
63#include <machine/reg.h>
64#include <machine/psl.h>
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/proc.h>
69#include <sys/errno.h>
70#include <sys/ptrace.h>
71#include <sys/uio.h>
72#include <sys/user.h>
73#include <sys/sysctl.h>
0b4e3aa0 74#include <sys/wait.h>
1c79356b
A
75
76#include <sys/mount.h>
77
78#include <kern/task.h>
79#include <kern/thread.h>
80#include <mach/machine/thread_status.h>
81
82/* Macros to clear/set/test flags. */
83#define SET(t, f) (t) |= (f)
84#define CLR(t, f) (t) &= ~(f)
85#define ISSET(t, f) ((t) & (f))
86
9bccf70c
A
87void psignal_lock __P((struct proc *, int, int));
88int isinferior __P((struct proc *, struct proc *));
89
1c79356b
A
90/*
91 * sys-trace system call.
92 */
93struct ptrace_args {
94 int req;
95 pid_t pid;
96 caddr_t addr;
97 int data;
98};
9bccf70c 99
1c79356b
A
100int
101ptrace(p, uap, retval)
102 struct proc *p;
103 struct ptrace_args *uap;
104 register_t *retval;
105{
106 struct proc *t = current_proc(); /* target process */
107 vm_offset_t start_addr, end_addr,
108 kern_addr, offset;
109 vm_size_t size;
1c79356b
A
110 task_t task;
111 thread_t thread;
112 thread_act_t th_act;
113 struct uthread *ut;
114 int *locr0;
115 int error = 0;
116#if defined(ppc)
de355530 117 struct ppc_thread_state statep;
1c79356b
A
118#elif defined(i386)
119 struct i386_saved_state statep;
120#else
121#error architecture not supported
122#endif
123 unsigned long state_count;
9bccf70c 124 int tr_sigexc = 0;
1c79356b
A
125
126
0b4e3aa0
A
127 if (uap->req == PT_DENY_ATTACH) {
128 if (ISSET(p->p_flag, P_TRACED)) {
129 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
9bccf70c 130 /* drop funnel before we return */
0b4e3aa0
A
131 thread_funnel_set(kernel_flock, FALSE);
132 thread_exception_return();
133 /* NOTREACHED */
134 }
135 SET(p->p_flag, P_NOATTACH);
136
137 return(0);
138 }
139
9bccf70c
A
140 if (uap->req == PT_FORCEQUOTA) {
141 if (is_suser()) {
142 SET(t->p_flag, P_FORCEQUOTA);
143 return (0);
144 } else
145 return (EPERM);
146 }
147
1c79356b
A
148 /*
149 * Intercept and deal with "please trace me" request.
150 */
151 if (uap->req == PT_TRACE_ME) {
152 SET(p->p_flag, P_TRACED);
153 /* Non-attached case, our tracer is our parent. */
154 t->p_oppid = t->p_pptr->p_pid;
155 return(0);
156 }
9bccf70c
A
157 if (uap->req == PT_SIGEXC) {
158 if (ISSET(p->p_flag, P_TRACED)) {
159 SET(p->p_flag, P_SIGEXC);
160 return(0);
161 } else
162 return(EINVAL);
163 }
1c79356b
A
164
165 /*
166 * Locate victim, and make sure it is traceable.
167 */
168 if ((t = pfind(uap->pid)) == NULL)
169 return (ESRCH);
170
171
172 /* We do not want ptrace to do anything with kernel, init
173 * and mach_init
174 */
175 if (uap->pid <=2 )
176 return (EPERM);
177
178 task = t->task;
9bccf70c
A
179 if (uap->req == PT_ATTACHEXC) {
180 uap->req = PT_ATTACH;
181 tr_sigexc = 1;
182 }
1c79356b
A
183 if (uap->req == PT_ATTACH) {
184
185 /*
186 * You can't attach to a process if:
187 * (1) it's the process that's doing the attaching,
188 */
189 if (t->p_pid == p->p_pid)
190 return (EINVAL);
191
192 /*
193 * (2) it's already being traced, or
194 */
195 if (ISSET(t->p_flag, P_TRACED))
196 return (EBUSY);
197
198 /*
199 * (3) it's not owned by you, or is set-id on exec
200 * (unless you're root).
201 */
202 if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
203 ISSET(t->p_flag, P_SUGID)) &&
204 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
205 return (error);
206
9bccf70c
A
207 if ((p->p_flag & P_TRACED) && isinferior(p, t))
208 return(EPERM);
209
0b4e3aa0
A
210 if (ISSET(t->p_flag, P_NOATTACH)) {
211 psignal(p, SIGSEGV);
212 return (EBUSY);
213 }
1c79356b 214 SET(t->p_flag, P_TRACED);
9bccf70c
A
215 if (tr_sigexc)
216 SET(t->p_flag, P_SIGEXC);
217
1c79356b
A
218 t->p_oppid = t->p_pptr->p_pid;
219 if (t->p_pptr != p)
220 proc_reparent(t, p);
221
222 if (get_task_userstop(task) == 0 ) {
223 t->p_xstat = 0;
224 psignal(t, SIGSTOP);
225 } else {
226 t->p_xstat = SIGSTOP;
227 task_resume(task);
228 }
229 return(0);
230 }
231
232 /*
233 * You can't do what you want to the process if:
234 * (1) It's not being traced at all,
235 */
236 if (!ISSET(t->p_flag, P_TRACED))
237 return (EPERM);
238
239 /*
240 * (2) it's not being traced by _you_, or
241 */
242 if (t->p_pptr != p)
243 return (EBUSY);
244
245 /*
246 * (3) it's not currently stopped.
247 */
248 if (t->p_stat != SSTOP)
249 return (EBUSY);
250
251 /*
252 * Mach version of ptrace executes request directly here,
253 * thus simplifying the interaction of ptrace and signals.
254 */
255 switch (uap->req) {
256
257 case PT_DETACH:
258 if (t->p_oppid != t->p_pptr->p_pid) {
259 struct proc *pp;
260
261 pp = pfind(t->p_oppid);
262 proc_reparent(t, pp ? pp : initproc);
263 }
264
265 t->p_oppid = 0;
266 CLR(t->p_flag, P_TRACED);
9bccf70c 267 CLR(t->p_flag, P_SIGEXC);
1c79356b
A
268 goto resume;
269
270 case PT_KILL:
271 /*
272 * Tell child process to kill itself after it
273 * is resumed by adding NSIG to p_cursig. [see issig]
274 */
9bccf70c 275 psignal_lock(t, SIGKILL, 0);
1c79356b
A
276 goto resume;
277
278 case PT_STEP: /* single step the child */
279 case PT_CONTINUE: /* continue the child */
280 th_act = (thread_act_t)get_firstthread(task);
9bccf70c 281 if (th_act == THR_ACT_NULL)
1c79356b
A
282 goto errorLabel;
283 ut = (uthread_t)get_bsdthread_info(th_act);
284 locr0 = ut->uu_ar0;
285#if defined(i386)
286 state_count = i386_NEW_THREAD_STATE_COUNT;
9bccf70c 287 if (thread_getstatus(th_act, i386_NEW_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
1c79356b
A
288 goto errorLabel;
289 }
290#elif defined(ppc)
de355530
A
291 state_count = PPC_THREAD_STATE_COUNT;
292 if (thread_getstatus(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
1c79356b
A
293 goto errorLabel;
294 }
295#else
296#error architecture not supported
297#endif
298 if ((int)uap->addr != 1) {
299#if defined(i386)
300 locr0[PC] = (int)uap->addr;
301#elif defined(ppc)
302#define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
303 if (!ALIGNED((int)uap->addr, sizeof(int)))
304 return (ERESTART);
305
de355530
A
306 statep.srr0 = (int)uap->addr;
307 state_count = PPC_THREAD_STATE_COUNT;
308 if (thread_setstatus(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
1c79356b
A
309 goto errorLabel;
310 }
311#undef ALIGNED
312#else
313#error architecture not implemented!
314#endif
315 } /* (int)uap->addr != 1 */
316
317 if ((unsigned)uap->data < 0 || (unsigned)uap->data >= NSIG)
318 goto errorLabel;
319
320 if (uap->data != 0) {
9bccf70c 321 psignal_lock(t, uap->data, 0);
1c79356b
A
322 }
323#if defined(ppc)
de355530
A
324 state_count = PPC_THREAD_STATE_COUNT;
325 if (thread_getstatus(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
1c79356b
A
326 goto errorLabel;
327 }
328#endif
329
330#define MSR_SE_BIT 21
331
332 if (uap->req == PT_STEP) {
333#if defined(i386)
334 locr0[PS] |= PSL_T;
335#elif defined(ppc)
336 statep.srr1 |= MASK(MSR_SE);
337#else
338#error architecture not implemented!
339#endif
340 } /* uap->req == PT_STEP */
341 else { /* PT_CONTINUE - clear trace bit if set */
342#if defined(i386)
343 locr0[PS] &= ~PSL_T;
344#elif defined(ppc)
345 statep.srr1 &= ~MASK(MSR_SE);
346#endif
347 }
348#if defined (ppc)
de355530
A
349 state_count = PPC_THREAD_STATE_COUNT;
350 if (thread_setstatus(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
1c79356b
A
351 goto errorLabel;
352 }
353#endif
354 resume:
355 t->p_xstat = uap->data;
356 t->p_stat = SRUN;
357 if (t->sigwait) {
358 wakeup((caddr_t)&(t->sigwait));
de355530 359 task_release(task);
1c79356b
A
360 }
361 break;
362
9bccf70c
A
363 case PT_THUPDATE: {
364 thread_act_t target_act;
365
366 if ((unsigned)uap->data >= NSIG)
367 goto errorLabel;
368 th_act = (thread_act_t)port_name_to_act((void *)uap->addr);
369 if (th_act == THR_ACT_NULL)
370 return (ESRCH);
371 ut = (uthread_t)get_bsdthread_info(th_act);
372 if (uap->data)
373 ut->uu_siglist |= sigmask(uap->data);
374 t->p_xstat = uap->data;
375 t->p_stat = SRUN;
376 act_deallocate(th_act);
377 return(0);
378 }
379 break;
380errorLabel:
1c79356b 381 default:
1c79356b
A
382 return(EINVAL);
383 }
9bccf70c 384
1c79356b
A
385 return(0);
386}
387