]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_process.c
xnu-201.14.tar.gz
[apple/xnu.git] / bsd / kern / mach_process.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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 */
62/*
63 * HISTORY
64 *
65 * 10-Jun-97 Umesh Vaishampayan (umeshv@apple.com)
66 * Ported to PPC. Cleaned up the architecture specific code.
67 */
68
69#include <machine/reg.h>
70#include <machine/psl.h>
71
72#include <sys/param.h>
73#include <sys/systm.h>
74#include <sys/proc.h>
75#include <sys/errno.h>
76#include <sys/ptrace.h>
77#include <sys/uio.h>
78#include <sys/user.h>
79#include <sys/sysctl.h>
0b4e3aa0 80#include <sys/wait.h>
1c79356b
A
81
82#include <sys/mount.h>
83
84#include <kern/task.h>
85#include <kern/thread.h>
86#include <mach/machine/thread_status.h>
87
88/* Macros to clear/set/test flags. */
89#define SET(t, f) (t) |= (f)
90#define CLR(t, f) (t) &= ~(f)
91#define ISSET(t, f) ((t) & (f))
92
93void psignal_lock __P((struct proc *, int, int, int));
94/*
95 * sys-trace system call.
96 */
97struct ptrace_args {
98 int req;
99 pid_t pid;
100 caddr_t addr;
101 int data;
102};
103int
104ptrace(p, uap, retval)
105 struct proc *p;
106 struct ptrace_args *uap;
107 register_t *retval;
108{
109 struct proc *t = current_proc(); /* target process */
110 vm_offset_t start_addr, end_addr,
111 kern_addr, offset;
112 vm_size_t size;
113 boolean_t change_protection;
114 task_t task;
115 thread_t thread;
116 thread_act_t th_act;
117 struct uthread *ut;
118 int *locr0;
119 int error = 0;
120#if defined(ppc)
121 struct ppc_saved_state statep;
122#elif defined(i386)
123 struct i386_saved_state statep;
124#else
125#error architecture not supported
126#endif
127 unsigned long state_count;
128
129
0b4e3aa0
A
130 if (uap->req == PT_DENY_ATTACH) {
131 if (ISSET(p->p_flag, P_TRACED)) {
132 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
133 /* drop funnel befewo we return */
134 thread_funnel_set(kernel_flock, FALSE);
135 thread_exception_return();
136 /* NOTREACHED */
137 }
138 SET(p->p_flag, P_NOATTACH);
139
140 return(0);
141 }
142
1c79356b
A
143 /*
144 * Intercept and deal with "please trace me" request.
145 */
146 if (uap->req == PT_TRACE_ME) {
147 SET(p->p_flag, P_TRACED);
148 /* Non-attached case, our tracer is our parent. */
149 t->p_oppid = t->p_pptr->p_pid;
150 return(0);
151 }
152
153 /*
154 * Locate victim, and make sure it is traceable.
155 */
156 if ((t = pfind(uap->pid)) == NULL)
157 return (ESRCH);
158
159
160 /* We do not want ptrace to do anything with kernel, init
161 * and mach_init
162 */
163 if (uap->pid <=2 )
164 return (EPERM);
165
166 task = t->task;
167 if (uap->req == PT_ATTACH) {
168
169 /*
170 * You can't attach to a process if:
171 * (1) it's the process that's doing the attaching,
172 */
173 if (t->p_pid == p->p_pid)
174 return (EINVAL);
175
176 /*
177 * (2) it's already being traced, or
178 */
179 if (ISSET(t->p_flag, P_TRACED))
180 return (EBUSY);
181
182 /*
183 * (3) it's not owned by you, or is set-id on exec
184 * (unless you're root).
185 */
186 if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
187 ISSET(t->p_flag, P_SUGID)) &&
188 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
189 return (error);
190
0b4e3aa0
A
191 if (ISSET(t->p_flag, P_NOATTACH)) {
192 psignal(p, SIGSEGV);
193 return (EBUSY);
194 }
1c79356b
A
195 SET(t->p_flag, P_TRACED);
196 t->p_oppid = t->p_pptr->p_pid;
197 if (t->p_pptr != p)
198 proc_reparent(t, p);
199
200 if (get_task_userstop(task) == 0 ) {
201 t->p_xstat = 0;
202 psignal(t, SIGSTOP);
203 } else {
204 t->p_xstat = SIGSTOP;
205 task_resume(task);
206 }
207 return(0);
208 }
209
210 /*
211 * You can't do what you want to the process if:
212 * (1) It's not being traced at all,
213 */
214 if (!ISSET(t->p_flag, P_TRACED))
215 return (EPERM);
216
217 /*
218 * (2) it's not being traced by _you_, or
219 */
220 if (t->p_pptr != p)
221 return (EBUSY);
222
223 /*
224 * (3) it's not currently stopped.
225 */
226 if (t->p_stat != SSTOP)
227 return (EBUSY);
228
229 /*
230 * Mach version of ptrace executes request directly here,
231 * thus simplifying the interaction of ptrace and signals.
232 */
233 switch (uap->req) {
234
235 case PT_DETACH:
236 if (t->p_oppid != t->p_pptr->p_pid) {
237 struct proc *pp;
238
239 pp = pfind(t->p_oppid);
240 proc_reparent(t, pp ? pp : initproc);
241 }
242
243 t->p_oppid = 0;
244 CLR(t->p_flag, P_TRACED);
245 goto resume;
246
247 case PT_KILL:
248 /*
249 * Tell child process to kill itself after it
250 * is resumed by adding NSIG to p_cursig. [see issig]
251 */
0b4e3aa0 252 psignal_lock(t, SIGKILL, 0, 0);
1c79356b
A
253 goto resume;
254
255 case PT_STEP: /* single step the child */
256 case PT_CONTINUE: /* continue the child */
257 th_act = (thread_act_t)get_firstthread(task);
258 if (th_act == THREAD_NULL)
259 goto errorLabel;
260 ut = (uthread_t)get_bsdthread_info(th_act);
261 locr0 = ut->uu_ar0;
262#if defined(i386)
263 state_count = i386_NEW_THREAD_STATE_COUNT;
264 if (act_machine_get_state(th_act, i386_NEW_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
265 goto errorLabel;
266 }
267#elif defined(ppc)
268 state_count = PPC_THREAD_STATE_COUNT;
269 if (act_machine_get_state(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
270 goto errorLabel;
271 }
272#else
273#error architecture not supported
274#endif
275 if ((int)uap->addr != 1) {
276#if defined(i386)
277 locr0[PC] = (int)uap->addr;
278#elif defined(ppc)
279#define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
280 if (!ALIGNED((int)uap->addr, sizeof(int)))
281 return (ERESTART);
282
283 statep.srr0 = (int)uap->addr;
284 state_count = PPC_THREAD_STATE_COUNT;
285 if (act_machine_set_state(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
286 goto errorLabel;
287 }
288#undef ALIGNED
289#else
290#error architecture not implemented!
291#endif
292 } /* (int)uap->addr != 1 */
293
294 if ((unsigned)uap->data < 0 || (unsigned)uap->data >= NSIG)
295 goto errorLabel;
296
297 if (uap->data != 0) {
298 psignal_lock(t, uap->data, 0, 1);
299 }
300#if defined(ppc)
301 state_count = PPC_THREAD_STATE_COUNT;
302 if (act_machine_get_state(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
303 goto errorLabel;
304 }
305#endif
306
307#define MSR_SE_BIT 21
308
309 if (uap->req == PT_STEP) {
310#if defined(i386)
311 locr0[PS] |= PSL_T;
312#elif defined(ppc)
313 statep.srr1 |= MASK(MSR_SE);
314#else
315#error architecture not implemented!
316#endif
317 } /* uap->req == PT_STEP */
318 else { /* PT_CONTINUE - clear trace bit if set */
319#if defined(i386)
320 locr0[PS] &= ~PSL_T;
321#elif defined(ppc)
322 statep.srr1 &= ~MASK(MSR_SE);
323#endif
324 }
325#if defined (ppc)
326 state_count = PPC_THREAD_STATE_COUNT;
327 if (act_machine_set_state(th_act, PPC_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
328 goto errorLabel;
329 }
330#endif
331 resume:
332 t->p_xstat = uap->data;
333 t->p_stat = SRUN;
334 if (t->sigwait) {
335 wakeup((caddr_t)&(t->sigwait));
336 task_release(task);
337 }
338 break;
339
340 default:
341 errorLabel:
342 return(EINVAL);
343 }
344 return(0);
345}
346