xref: /netbsd-src/sys/compat/linux/arch/powerpc/linux_ptrace.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: linux_ptrace.c,v 1.16 2007/07/09 21:10:46 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.16 2007/07/09 21:10:46 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
107 linux_sys_ptrace_arch(l, v, retval)	/* XXX Check me! (From NetBSD/i386) */
108 	struct lwp *l;
109 	void *v;
110 	register_t *retval;
111 {
112 	struct linux_sys_ptrace_args /* {
113 		syscallarg(int) request;
114 		syscallarg(int) pid;
115 		syscallarg(int) addr;
116 		syscallarg(int) data;
117 	} */ *uap = v;
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 	switch (request = SCARG(uap, request)) {
130 		case LINUX_PTRACE_PEEKUSR:
131 		case LINUX_PTRACE_POKEUSR:
132 		case LINUX_PTRACE_GETREGS:
133 		case LINUX_PTRACE_SETREGS:
134 		case LINUX_PTRACE_GETFPREGS:
135 		case LINUX_PTRACE_SETFPREGS:
136 			break;
137 		default:
138 			return EIO;
139 	}
140 
141 	/* Find the process we're supposed to be operating on. */
142 	if ((t = pfind(SCARG(uap, pid))) == NULL)
143 		return ESRCH;
144 
145 	/*
146 	 * You can't do what you want to the process if:
147 	 *	(1) It's not being traced at all,
148 	 */
149 	if (!ISSET(t->p_slflag, PSL_TRACED))	/* XXXSMP */
150 		return EPERM;
151 
152 	/*
153 	 *	(2) it's being traced by procfs (which has
154 	 *		 different signal delivery semantics),
155 	 */
156 	if (ISSET(t->p_slflag, PSL_FSTRACE))
157 		return EBUSY;
158 
159 	/*
160 	 *	(3) it's not being traced by _you_, or
161 	 */
162 	if (t->p_pptr != p)
163 		return EBUSY;
164 
165 	/*
166 	 *	(4) it's not currently stopped.
167 	 */
168 	if (t->p_stat != SSTOP || !t->p_waited)
169 		return EBUSY;
170 
171 	lt = LIST_FIRST(&t->p_lwps);
172 	*retval = 0;
173 
174 	switch (request) {
175 	case  LINUX_PTRACE_GETREGS:
176 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
177 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
178 		    M_TEMP, M_WAITOK);
179 
180 		error = process_read_regs(lt, regs);
181 		if (error != 0)
182 			goto out;
183 
184 		for (i=0; i<=31; i++)
185 			linux_regs->lgpr[i] = regs->fixreg[i];
186 		linux_regs->lnip = regs->pc;
187 		linux_regs->lmsr = 0;
188 		/* XXX Is that right? */
189 		linux_regs->lorig_gpr3 = regs->fixreg[3];
190 		linux_regs->lctr = regs->ctr;
191 		linux_regs->llink = regs->lr;
192 		linux_regs->lxer = regs->xer;
193 		linux_regs->lccr = regs->cr;
194 		linux_regs->lmq = 0;
195 		linux_regs->ltrap = 0;
196 		linux_regs->ldar = 0;
197 		linux_regs->ldsisr = 0;
198 		linux_regs->lresult = 0;
199 
200 		error = copyout(linux_regs, (void *)SCARG(uap, data),
201 			 sizeof(struct linux_pt_regs));
202 		goto out;
203 
204 	case  LINUX_PTRACE_SETREGS:
205 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
206 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
207 		    M_TEMP, M_WAITOK);
208 
209 		error = copyin((void *)SCARG(uap, data), linux_regs,
210 			 sizeof(struct linux_pt_regs));
211 		if (error != 0)
212 			goto out;
213 
214 		for (i=0; i<=31; i++)
215 			regs->fixreg[i] = linux_regs->lgpr[i];
216 		regs->lr = linux_regs->llink;
217 		regs->cr = linux_regs->lccr;
218 		regs->xer = linux_regs->lxer;
219 		regs->ctr = linux_regs->lctr;
220 		regs->pc = linux_regs->lnip; /* XXX */
221 
222 		error = process_write_regs(lt, regs);
223 		goto out;
224 
225 	case  LINUX_PTRACE_GETFPREGS:
226 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
227 		    M_TEMP, M_WAITOK);
228 		MALLOC(linux_fpreg, double *,
229 		    32*sizeof(double), M_TEMP, M_WAITOK);
230 
231 		error = process_read_fpregs(lt, fpregs);
232 		if (error != 0)
233 			goto out;
234 
235 		/* zero the contents if NetBSD fpreg structure is smaller */
236 		if (sizeof(struct fpreg) < (32*sizeof(double)))
237 			memset(linux_fpreg, '\0', (32*sizeof(double)));
238 
239 		memcpy(linux_fpreg, fpregs,
240 			min(32*sizeof(double), sizeof(struct fpreg)));
241 		error = copyout(linux_fpreg, (void *)SCARG(uap, data),
242 			 32*sizeof(double));
243 		goto out;
244 
245 	case  LINUX_PTRACE_SETFPREGS:
246 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
247 		    M_TEMP, M_WAITOK);
248 		MALLOC(linux_fpreg, double *,
249 		    32*sizeof(double), M_TEMP, M_WAITOK);
250 		error = copyin((void *)SCARG(uap, data), linux_fpreg,
251 			 32*sizeof(double));
252 		if (error != 0)
253 			goto out;
254 
255 		memset(fpregs, '\0', sizeof(struct fpreg));
256 		memcpy(fpregs, linux_fpreg,
257 		    min(32*sizeof(double), sizeof(struct fpreg)));
258 
259 		error = process_write_fpregs(lt, fpregs);
260 		goto out;
261 
262 	case  LINUX_PTRACE_PEEKUSR:
263 		addr = SCARG(uap, addr);
264 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
265 		error = process_read_regs(lt, regs);
266 		if (error)
267 			goto out;
268 
269 		uvm_lwp_hold(lt);	/* need full process info */
270 		error = 0;
271 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
272 		    (addr > LUSR_OFF(lu_comm_end)))
273 		 	error = 1;
274 		else if (addr == LUSR_OFF(u_tsize))
275 			*retval = p->p_vmspace->vm_tsize;
276 		else if (addr == LUSR_OFF(u_dsize))
277 			*retval = p->p_vmspace->vm_dsize;
278 		else if (addr == LUSR_OFF(u_ssize))
279 			*retval = p->p_vmspace->vm_ssize;
280 		else if (addr == LUSR_OFF(start_code))
281 			*retval = (register_t) p->p_vmspace->vm_taddr;
282 		else if (addr == LUSR_OFF(start_stack))
283 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
284 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
285 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
286 			*retval = regs->fixreg[addr / sizeof (register_t)];
287 		else if (addr == LUSR_REG_OFF(lnip))
288 			*retval = regs->pc;
289 		else if (addr == LUSR_REG_OFF(lctr))
290 			*retval = regs->ctr;
291 		else if (addr == LUSR_REG_OFF(llink))
292 			*retval = regs->lr;
293 		else if (addr == LUSR_REG_OFF(lxer))
294 			*retval = regs->xer;
295 		else if (addr == LUSR_REG_OFF(lccr))
296 			*retval = regs->cr;
297 		else if (addr == LUSR_OFF(signal))
298 			error = 1;
299 		else if (addr == LUSR_OFF(signal))
300 			error = 1;
301 		else if (addr == LUSR_OFF(magic))
302 			error = 1;
303 		else if (addr == LUSR_OFF(u_comm))
304 			error = 1;
305 		else {
306 #ifdef DEBUG_LINUX
307 			printf("linux_ptrace: unsupported address: %d\n", addr);
308 #endif
309 			error = 1;
310 		}
311 
312 		uvm_lwp_rele(lt);
313 
314 		if (error)
315 			goto out;
316 
317 		error = copyout (retval,
318 		    (void *)SCARG(uap, data), sizeof retval);
319 		*retval = SCARG(uap, data);
320 
321 		goto out;
322 
323 		break;
324 
325 	case  LINUX_PTRACE_POKEUSR: /* XXX Not tested */
326 		addr = SCARG(uap, addr);
327 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
328 		error = process_read_regs(lt, regs);
329 		if (error)
330 			goto out;
331 
332 		uvm_lwp_hold(lt);       /* need full process info */
333 		error = 0;
334 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
335 		    (addr > LUSR_OFF(lu_comm_end)))
336 		 	error = 1;
337 		else if (addr == LUSR_OFF(u_tsize))
338 			error = 1;
339 		else if (addr == LUSR_OFF(u_dsize))
340 			error = 1;
341 		else if (addr == LUSR_OFF(u_ssize))
342 			error = 1;
343 		else if (addr == LUSR_OFF(start_code))
344 			error = 1;
345 		else if (addr == LUSR_OFF(start_stack))
346 			error = 1;
347 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
348 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
349 			regs->fixreg[addr / sizeof (register_t)] =
350 			    (register_t)SCARG(uap, data);
351 		else if (addr == LUSR_REG_OFF(lnip))
352 			regs->pc = (register_t)SCARG(uap, data);
353 		else if (addr == LUSR_REG_OFF(lctr))
354 			regs->ctr = (register_t)SCARG(uap, data);
355 		else if (addr == LUSR_REG_OFF(llink))
356 			regs->lr = (register_t)SCARG(uap, data);
357 		else if (addr == LUSR_REG_OFF(lxer))
358 			regs->xer = (register_t)SCARG(uap, data);
359 		else if (addr == LUSR_REG_OFF(lccr))
360 			regs->cr = (register_t)SCARG(uap, data);
361 		else if (addr == LUSR_OFF(signal))
362 			error = 1;
363 		else if (addr == LUSR_OFF(magic))
364 			error = 1;
365 		else if (addr == LUSR_OFF(u_comm))
366 			error = 1;
367 		else {
368 #ifdef DEBUG_LINUX
369 			printf("linux_ptrace: unsupported address: %d\n", addr);
370 #endif
371 			error = 1;
372 		}
373 
374 		uvm_lwp_rele(lt);
375 
376 		error = process_write_regs(lt,regs);
377 		if (error)
378 			goto out;
379 
380 		break;
381 	default:
382 		/* never reached */
383 		break;
384 	}
385 	return EIO;
386 
387 	 out:
388 	if (regs)
389 		FREE(regs, M_TEMP);
390 	if (fpregs)
391 		FREE(fpregs, M_TEMP);
392 	if (linux_regs)
393 		FREE(linux_regs, M_TEMP);
394 	if (linux_fpreg)
395 		FREE(linux_fpreg, M_TEMP);
396 	return (error);
397 }
398