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