xref: /netbsd-src/sys/compat/linux/arch/powerpc/linux_ptrace.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: linux_ptrace.c,v 1.18 2008/04/23 14:18:50 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matthias Scheler and Emmanuel Dreyfus.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.18 2008/04/23 14:18:50 ad Exp $");
41 
42 #include "opt_ptrace.h"
43 
44 #include <sys/param.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/proc.h>
48 #include <sys/ptrace.h>
49 #include <sys/systm.h>
50 #include <sys/syscallargs.h>
51 #include <uvm/uvm_extern.h>
52 
53 #include <machine/reg.h>
54 
55 #include <compat/linux/common/linux_types.h>
56 #include <compat/linux/common/linux_ptrace.h>
57 #include <compat/linux/common/linux_signal.h>
58 
59 #include <compat/linux/common/linux_util.h>
60 #include <compat/linux/common/linux_machdep.h>
61 #include <compat/linux/common/linux_emuldata.h>
62 #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
63 
64 #include <compat/linux/linux_syscallargs.h>
65 
66 #include <lib/libkern/libkern.h>	/* for offsetof() */
67 
68 /*
69  * From Linux's include/asm-ppc/ptrace.h.
70  * structure used for storing reg context:  defined in linux_machdep.h
71  * structure used for storing floating point context:
72  * Linux just uses a double fpr[32], no struct
73  */
74 
75 /*
76  * user struct for linux process - this is used for Linux ptrace emulation
77  * most of it is junk only used by gdb
78  *
79  * From Linux's include/asm-ppc/user.h
80  *
81  * XXX u_ar0 was a struct reg in Linux/powerpc
82  * Can't find out what a struct regs is for Linux/powerpc,
83  * so we use a struct pt_regs instead. don't know if this is right.
84  */
85 
86 struct linux_user {
87 	struct linux_pt_regs regs;
88 #define lusr_startgdb regs
89 	size_t u_tsize;
90 	size_t u_dsize;
91 	size_t u_ssize;
92 	unsigned long start_code;
93 	unsigned long start_data;
94 	unsigned long start_stack;
95 	long int signal;
96 	struct linux_pt_regs *u_ar0;		/* help gdb find registers */
97 	unsigned long magic;
98 	char u_comm[32];
99 #define lu_comm_end u_comm[31]
100 };
101 
102 #define LUSR_OFF(member) offsetof(struct linux_user, member)
103 #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member)
104 #define ISSET(t, f) ((t) & (f))
105 
106 int linux_ptrace_disabled = 1;	/* bitrotted */
107 
108 /* XXX Check me! (From NetBSD/i386) */
109 int
110 linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
111 {
112 	/* {
113 		syscallarg(int) request;
114 		syscallarg(int) pid;
115 		syscallarg(int) addr;
116 		syscallarg(int) data;
117 	} */
118 	int request, error;
119 	struct proc *p = l->l_proc;
120 	struct proc *t;				/* target process */
121 	struct lwp *lt;
122 	struct reg *regs = NULL;
123 	struct fpreg *fpregs = NULL;
124 	struct linux_pt_regs *linux_regs = NULL;
125 	double *linux_fpreg = NULL;  /* it's an array, not a struct */
126 	int addr;
127 	int i;
128 
129 	if (linux_ptrace_disabled)
130 		return ENOSYS;
131 
132 	switch (request = SCARG(uap, request)) {
133 		case LINUX_PTRACE_PEEKUSR:
134 		case LINUX_PTRACE_POKEUSR:
135 		case LINUX_PTRACE_GETREGS:
136 		case LINUX_PTRACE_SETREGS:
137 		case LINUX_PTRACE_GETFPREGS:
138 		case LINUX_PTRACE_SETFPREGS:
139 			break;
140 		default:
141 			return EIO;
142 	}
143 
144 	/* Find the process we're supposed to be operating on. */
145 	if ((t = pfind(SCARG(uap, pid))) == NULL)
146 		return ESRCH;
147 
148 	/*
149 	 * You can't do what you want to the process if:
150 	 *	(1) It's not being traced at all,
151 	 */
152 	if (!ISSET(t->p_slflag, PSL_TRACED))	/* XXXSMP */
153 		return EPERM;
154 
155 	/*
156 	 *	(2) it's being traced by procfs (which has
157 	 *		 different signal delivery semantics),
158 	 */
159 	if (ISSET(t->p_slflag, PSL_FSTRACE))
160 		return EBUSY;
161 
162 	/*
163 	 *	(3) it's not being traced by _you_, or
164 	 */
165 	if (t->p_pptr != p)
166 		return EBUSY;
167 
168 	/*
169 	 *	(4) it's not currently stopped.
170 	 */
171 	if (t->p_stat != SSTOP || !t->p_waited)
172 		return EBUSY;
173 
174 	lt = LIST_FIRST(&t->p_lwps);
175 	*retval = 0;
176 
177 	switch (request) {
178 	case  LINUX_PTRACE_GETREGS:
179 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
180 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
181 		    M_TEMP, M_WAITOK);
182 
183 		error = process_read_regs(lt, regs);
184 		if (error != 0)
185 			goto out;
186 
187 		for (i=0; i<=31; i++)
188 			linux_regs->lgpr[i] = regs->fixreg[i];
189 		linux_regs->lnip = regs->pc;
190 		linux_regs->lmsr = 0;
191 		/* XXX Is that right? */
192 		linux_regs->lorig_gpr3 = regs->fixreg[3];
193 		linux_regs->lctr = regs->ctr;
194 		linux_regs->llink = regs->lr;
195 		linux_regs->lxer = regs->xer;
196 		linux_regs->lccr = regs->cr;
197 		linux_regs->lmq = 0;
198 		linux_regs->ltrap = 0;
199 		linux_regs->ldar = 0;
200 		linux_regs->ldsisr = 0;
201 		linux_regs->lresult = 0;
202 
203 		error = copyout(linux_regs, (void *)SCARG(uap, data),
204 			 sizeof(struct linux_pt_regs));
205 		goto out;
206 
207 	case  LINUX_PTRACE_SETREGS:
208 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
209 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
210 		    M_TEMP, M_WAITOK);
211 
212 		error = copyin((void *)SCARG(uap, data), linux_regs,
213 			 sizeof(struct linux_pt_regs));
214 		if (error != 0)
215 			goto out;
216 
217 		for (i=0; i<=31; i++)
218 			regs->fixreg[i] = linux_regs->lgpr[i];
219 		regs->lr = linux_regs->llink;
220 		regs->cr = linux_regs->lccr;
221 		regs->xer = linux_regs->lxer;
222 		regs->ctr = linux_regs->lctr;
223 		regs->pc = linux_regs->lnip; /* XXX */
224 
225 		error = process_write_regs(lt, regs);
226 		goto out;
227 
228 	case  LINUX_PTRACE_GETFPREGS:
229 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
230 		    M_TEMP, M_WAITOK);
231 		MALLOC(linux_fpreg, double *,
232 		    32*sizeof(double), M_TEMP, M_WAITOK);
233 
234 		error = process_read_fpregs(lt, fpregs);
235 		if (error != 0)
236 			goto out;
237 
238 		/* zero the contents if NetBSD fpreg structure is smaller */
239 		if (sizeof(struct fpreg) < (32*sizeof(double)))
240 			memset(linux_fpreg, '\0', (32*sizeof(double)));
241 
242 		memcpy(linux_fpreg, fpregs,
243 			min(32*sizeof(double), sizeof(struct fpreg)));
244 		error = copyout(linux_fpreg, (void *)SCARG(uap, data),
245 			 32*sizeof(double));
246 		goto out;
247 
248 	case  LINUX_PTRACE_SETFPREGS:
249 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
250 		    M_TEMP, M_WAITOK);
251 		MALLOC(linux_fpreg, double *,
252 		    32*sizeof(double), M_TEMP, M_WAITOK);
253 		error = copyin((void *)SCARG(uap, data), linux_fpreg,
254 			 32*sizeof(double));
255 		if (error != 0)
256 			goto out;
257 
258 		memset(fpregs, '\0', sizeof(struct fpreg));
259 		memcpy(fpregs, linux_fpreg,
260 		    min(32*sizeof(double), sizeof(struct fpreg)));
261 
262 		error = process_write_fpregs(lt, fpregs);
263 		goto out;
264 
265 	case  LINUX_PTRACE_PEEKUSR:
266 		addr = SCARG(uap, addr);
267 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
268 		error = process_read_regs(lt, regs);
269 		if (error)
270 			goto out;
271 
272 		uvm_lwp_hold(lt);	/* need full process info */
273 		error = 0;
274 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
275 		    (addr > LUSR_OFF(lu_comm_end)))
276 		 	error = 1;
277 		else if (addr == LUSR_OFF(u_tsize))
278 			*retval = p->p_vmspace->vm_tsize;
279 		else if (addr == LUSR_OFF(u_dsize))
280 			*retval = p->p_vmspace->vm_dsize;
281 		else if (addr == LUSR_OFF(u_ssize))
282 			*retval = p->p_vmspace->vm_ssize;
283 		else if (addr == LUSR_OFF(start_code))
284 			*retval = (register_t) p->p_vmspace->vm_taddr;
285 		else if (addr == LUSR_OFF(start_stack))
286 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
287 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
288 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
289 			*retval = regs->fixreg[addr / sizeof (register_t)];
290 		else if (addr == LUSR_REG_OFF(lnip))
291 			*retval = regs->pc;
292 		else if (addr == LUSR_REG_OFF(lctr))
293 			*retval = regs->ctr;
294 		else if (addr == LUSR_REG_OFF(llink))
295 			*retval = regs->lr;
296 		else if (addr == LUSR_REG_OFF(lxer))
297 			*retval = regs->xer;
298 		else if (addr == LUSR_REG_OFF(lccr))
299 			*retval = regs->cr;
300 		else if (addr == LUSR_OFF(signal))
301 			error = 1;
302 		else if (addr == LUSR_OFF(signal))
303 			error = 1;
304 		else if (addr == LUSR_OFF(magic))
305 			error = 1;
306 		else if (addr == LUSR_OFF(u_comm))
307 			error = 1;
308 		else {
309 #ifdef DEBUG_LINUX
310 			printf("linux_ptrace: unsupported address: %d\n", addr);
311 #endif
312 			error = 1;
313 		}
314 
315 		uvm_lwp_rele(lt);
316 
317 		if (error)
318 			goto out;
319 
320 		error = copyout (retval,
321 		    (void *)SCARG(uap, data), sizeof retval);
322 		*retval = SCARG(uap, data);
323 
324 		goto out;
325 
326 		break;
327 
328 	case  LINUX_PTRACE_POKEUSR: /* XXX Not tested */
329 		addr = SCARG(uap, addr);
330 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
331 		error = process_read_regs(lt, regs);
332 		if (error)
333 			goto out;
334 
335 		uvm_lwp_hold(lt);       /* need full process info */
336 		error = 0;
337 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
338 		    (addr > LUSR_OFF(lu_comm_end)))
339 		 	error = 1;
340 		else if (addr == LUSR_OFF(u_tsize))
341 			error = 1;
342 		else if (addr == LUSR_OFF(u_dsize))
343 			error = 1;
344 		else if (addr == LUSR_OFF(u_ssize))
345 			error = 1;
346 		else if (addr == LUSR_OFF(start_code))
347 			error = 1;
348 		else if (addr == LUSR_OFF(start_stack))
349 			error = 1;
350 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
351 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
352 			regs->fixreg[addr / sizeof (register_t)] =
353 			    (register_t)SCARG(uap, data);
354 		else if (addr == LUSR_REG_OFF(lnip))
355 			regs->pc = (register_t)SCARG(uap, data);
356 		else if (addr == LUSR_REG_OFF(lctr))
357 			regs->ctr = (register_t)SCARG(uap, data);
358 		else if (addr == LUSR_REG_OFF(llink))
359 			regs->lr = (register_t)SCARG(uap, data);
360 		else if (addr == LUSR_REG_OFF(lxer))
361 			regs->xer = (register_t)SCARG(uap, data);
362 		else if (addr == LUSR_REG_OFF(lccr))
363 			regs->cr = (register_t)SCARG(uap, data);
364 		else if (addr == LUSR_OFF(signal))
365 			error = 1;
366 		else if (addr == LUSR_OFF(magic))
367 			error = 1;
368 		else if (addr == LUSR_OFF(u_comm))
369 			error = 1;
370 		else {
371 #ifdef DEBUG_LINUX
372 			printf("linux_ptrace: unsupported address: %d\n", addr);
373 #endif
374 			error = 1;
375 		}
376 
377 		uvm_lwp_rele(lt);
378 
379 		error = process_write_regs(lt,regs);
380 		if (error)
381 			goto out;
382 
383 		break;
384 	default:
385 		/* never reached */
386 		break;
387 	}
388 	return EIO;
389 
390 	 out:
391 	if (regs)
392 		FREE(regs, M_TEMP);
393 	if (fpregs)
394 		FREE(fpregs, M_TEMP);
395 	if (linux_regs)
396 		FREE(linux_regs, M_TEMP);
397 	if (linux_fpreg)
398 		FREE(linux_fpreg, M_TEMP);
399 	return (error);
400 }
401