xref: /netbsd-src/sys/compat/linux/arch/powerpc/linux_ptrace.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: linux_ptrace.c,v 1.7 2001/06/02 13:49:44 manu 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/types.h>
40 #include <sys/param.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/proc.h>
44 #include <sys/ptrace.h>
45 #include <sys/systm.h>
46 #include <sys/syscallargs.h>
47 #include <uvm/uvm_extern.h>
48 
49 #include <machine/reg.h>
50 
51 #include <compat/linux/common/linux_types.h>
52 #include <compat/linux/common/linux_ptrace.h>
53 #include <compat/linux/common/linux_signal.h>
54 
55 #include <compat/linux/common/linux_util.h>
56 #include <compat/linux/common/linux_machdep.h>
57 #include <compat/linux/common/linux_emuldata.h>
58 #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
59 
60 #include <compat/linux/linux_syscallargs.h>
61 
62 #include <lib/libkern/libkern.h>	/* for offsetof() */
63 
64 /*
65  * From Linux's include/asm-ppc/ptrace.h.
66  * structure used for storing reg context:  defined in linux_machdep.h
67  * structure used for storing floating point context:
68  * Linux just uses a double fpr[32], no struct
69  */
70 
71 /*
72  * user struct for linux process - this is used for Linux ptrace emulation
73  * most of it is junk only used by gdb
74  *
75  * From Linux's include/asm-ppc/user.h
76  *
77  * XXX u_ar0 was a struct reg in Linux/powerpc
78  * Can't find out what a struct regs is for Linux/powerpc,
79  * so we use a struct pt_regs instead. don't know if this is right.
80  */
81 
82 struct linux_user {
83 	struct linux_pt_regs regs;
84 #define lusr_startgdb regs
85 	size_t u_tsize;
86 	size_t u_dsize;
87 	size_t u_ssize;
88 	unsigned long start_code;
89 	unsigned long start_data;
90 	unsigned long start_stack;
91 	long int signal;
92 	struct linux_pt_regs *u_ar0;		/* help gdb find registers */
93 	unsigned long magic;
94 	char u_comm[32];
95 #define lu_comm_end u_comm[31]
96 };
97 
98 #define LUSR_OFF(member) offsetof(struct linux_user, member)
99 #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member)
100 #define ISSET(t, f) ((t) & (f))
101 
102 int
103 linux_sys_ptrace_arch(p, v, retval)	/* XXX Check me! (From NetBSD/i386) */
104 	struct proc *p;
105 	void *v;
106 	register_t *retval;
107 {
108 	struct linux_sys_ptrace_args /* {
109 		syscallarg(int) request;
110 		syscallarg(int) pid;
111 		syscallarg(int) addr;
112 		syscallarg(int) data;
113 	} */ *uap = v;
114 	int request, error;
115 	struct proc *t;				/* target process */
116 	struct reg *regs = NULL;
117 	struct fpreg *fpregs = NULL;
118 	struct linux_pt_regs *linux_regs = NULL;
119 	double *linux_fpreg = NULL;  /* it's an array, not a struct */
120 	int addr;
121 	int i;
122 
123 	switch (request = SCARG(uap, request)) {
124 		case LINUX_PTRACE_PEEKUSR:
125 		case LINUX_PTRACE_POKEUSR:
126 		case LINUX_PTRACE_GETREGS:
127 		case LINUX_PTRACE_SETREGS:
128 		case LINUX_PTRACE_GETFPREGS:
129 		case LINUX_PTRACE_SETFPREGS:
130 			break;
131 		default:
132 			return EIO;
133 	}
134 
135 	/* Find the process we're supposed to be operating on. */
136 	if ((t = pfind(SCARG(uap, pid))) == NULL)
137 		return ESRCH;
138 
139 	/*
140 	 * You can't do what you want to the process if:
141 	 *	(1) It's not being traced at all,
142 	 */
143 	if (!ISSET(t->p_flag, P_TRACED))
144 		return EPERM;
145 
146 	/*
147 	 *	(2) it's being traced by procfs (which has
148 	 *		 different signal delivery semantics),
149 	 */
150 	if (ISSET(t->p_flag, P_FSTRACE))
151 		return EBUSY;
152 
153 	/*
154 	 *	(3) it's not being traced by _you_, or
155 	 */
156 	if (t->p_pptr != p)
157 		return EBUSY;
158 
159 	/*
160 	 *	(4) it's not currently stopped.
161 	 */
162 	if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
163 		return EBUSY;
164 
165 	*retval = 0;
166 
167 	switch (request) {
168 	case  LINUX_PTRACE_GETREGS:
169 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
170 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
171 		    M_TEMP, M_WAITOK);
172 
173 		error = process_read_regs(t, regs);
174 		if (error != 0)
175 			goto out;
176 
177 		for (i=0; i<=31; i++)
178 			linux_regs->lgpr[i] = regs->fixreg[i];
179 		linux_regs->lnip = regs->pc;
180 		linux_regs->lmsr = 0;
181 		/* XXX Is that right? */
182 		linux_regs->lorig_gpr3 = regs->fixreg[3];
183 		linux_regs->lctr = regs->ctr;
184 		linux_regs->llink = regs->lr;
185 		linux_regs->lxer = regs->xer;
186 		linux_regs->lccr = regs->cr;
187 		linux_regs->lmq = 0;
188 		linux_regs->ltrap = 0;
189 		linux_regs->ldar = 0;
190 		linux_regs->ldsisr = 0;
191 		linux_regs->lresult = 0;
192 
193 		error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
194 			 sizeof(struct linux_pt_regs));
195 		goto out;
196 
197 	case  LINUX_PTRACE_SETREGS:
198 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
199 		MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
200 		    M_TEMP, M_WAITOK);
201 
202 		error = copyin((caddr_t)SCARG(uap, data), linux_regs,
203 			 sizeof(struct linux_pt_regs));
204 		if (error != 0)
205 			goto out;
206 
207 		for (i=0; i<=31; i++)
208 			regs->fixreg[i] = linux_regs->lgpr[i];
209 		regs->lr = linux_regs->llink;
210 		regs->cr = linux_regs->lccr;
211 		regs->xer = linux_regs->lxer;
212 		regs->ctr = linux_regs->lctr;
213 		regs->pc = linux_regs->lnip; /* XXX */
214 
215 		error = process_write_regs(t, regs);
216 		goto out;
217 
218 	case  LINUX_PTRACE_GETFPREGS:
219 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
220 		    M_TEMP, M_WAITOK);
221 		MALLOC(linux_fpreg, double *,
222 		    32*sizeof(double), M_TEMP, M_WAITOK);
223 
224 		error = process_read_fpregs(t, fpregs);
225 		if (error != 0)
226 			goto out;
227 
228 		/* zero the contents if NetBSD fpreg structure is smaller */
229 		if (sizeof(struct fpreg) < (32*sizeof(double)))
230 			memset(linux_fpreg, '\0', (32*sizeof(double)));
231 
232 		memcpy(linux_fpreg, fpregs,
233 			min(32*sizeof(double), sizeof(struct fpreg)));
234 		error = copyout(linux_fpreg, (caddr_t)SCARG(uap, data),
235 			 32*sizeof(double));
236 		goto out;
237 
238 	case  LINUX_PTRACE_SETFPREGS:
239 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
240 		    M_TEMP, M_WAITOK);
241 		MALLOC(linux_fpreg, double *,
242 		    32*sizeof(double), M_TEMP, M_WAITOK);
243 		error = copyin((caddr_t)SCARG(uap, data), linux_fpreg,
244 			 32*sizeof(double));
245 		if (error != 0)
246 			goto out;
247 
248 		memset(fpregs, '\0', sizeof(struct fpreg));
249 		memcpy(fpregs, linux_fpreg,
250 		    min(32*sizeof(double), sizeof(struct fpreg)));
251 
252 		error = process_write_fpregs(t, fpregs);
253 		goto out;
254 
255 	case  LINUX_PTRACE_PEEKUSR:
256 		addr = SCARG(uap, addr);
257 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
258 		error = process_read_regs(t, regs);
259 		if (error)
260 			goto out;
261 
262 		PHOLD(t);	/* need full process info */
263 		error = 0;
264 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
265 		    (addr > LUSR_OFF(lu_comm_end)))
266 		 	error = 1;
267 		else if (addr == LUSR_OFF(u_tsize))
268 			*retval = p->p_vmspace->vm_tsize;
269 		else if (addr == LUSR_OFF(u_dsize))
270 			*retval = p->p_vmspace->vm_dsize;
271 		else if (addr == LUSR_OFF(u_ssize))
272 			*retval = p->p_vmspace->vm_ssize;
273 		else if (addr == LUSR_OFF(start_code))
274 			*retval = (register_t) p->p_vmspace->vm_taddr;
275 		else if (addr == LUSR_OFF(start_stack))
276 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
277 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
278 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
279 			*retval = regs->fixreg[addr / sizeof (register_t)];
280 		else if (addr == LUSR_REG_OFF(lnip))
281 			*retval = regs->pc;
282 		else if (addr == LUSR_REG_OFF(lctr))
283 			*retval = regs->ctr;
284 		else if (addr == LUSR_REG_OFF(llink))
285 			*retval = regs->lr;
286 		else if (addr == LUSR_REG_OFF(lxer))
287 			*retval = regs->xer;
288 		else if (addr == LUSR_REG_OFF(lccr))
289 			*retval = regs->cr;
290 		else if (addr == LUSR_OFF(signal))
291 			error = 1;
292 		else if (addr == LUSR_OFF(signal))
293 			error = 1;
294 		else if (addr == LUSR_OFF(magic))
295 			error = 1;
296 		else if (addr == LUSR_OFF(u_comm))
297 			error = 1;
298 		else {
299 #ifdef DEBUG_LINUX
300 			printf("linux_ptrace: unsupported address: %d\n", addr);
301 #endif
302 			error = 1;
303 		}
304 
305 		PRELE(t);
306 
307 		if (error)
308 			goto out;
309 
310 		error = copyout (retval,
311 		    (caddr_t)SCARG(uap, data), sizeof retval);
312 		*retval = SCARG(uap, data);
313 
314 		goto out;
315 
316 		break;
317 
318 	case  LINUX_PTRACE_POKEUSR: /* XXX Not tested */
319 		addr = SCARG(uap, addr);
320 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
321 		error = process_read_regs(t, regs);
322 		if (error)
323 			goto out;
324 
325 		PHOLD(t);       /* need full process info */
326 		error = 0;
327 		if ((addr < LUSR_OFF(lusr_startgdb)) ||
328 		    (addr > LUSR_OFF(lu_comm_end)))
329 		 	error = 1;
330 		else if (addr == LUSR_OFF(u_tsize))
331 			error = 1;
332 		else if (addr == LUSR_OFF(u_dsize))
333 			error = 1;
334 		else if (addr == LUSR_OFF(u_ssize))
335 			error = 1;
336 		else if (addr == LUSR_OFF(start_code))
337 			error = 1;
338 		else if (addr == LUSR_OFF(start_stack))
339 			error = 1;
340 		else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
341 		    (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
342 			regs->fixreg[addr / sizeof (register_t)] =
343 			    (register_t)SCARG(uap, data);
344 		else if (addr == LUSR_REG_OFF(lnip))
345 			regs->pc = (register_t)SCARG(uap, data);
346 		else if (addr == LUSR_REG_OFF(lctr))
347 			regs->ctr = (register_t)SCARG(uap, data);
348 		else if (addr == LUSR_REG_OFF(llink))
349 			regs->lr = (register_t)SCARG(uap, data);
350 		else if (addr == LUSR_REG_OFF(lxer))
351 			regs->xer = (register_t)SCARG(uap, data);
352 		else if (addr == LUSR_REG_OFF(lccr))
353 			regs->cr = (register_t)SCARG(uap, data);
354 		else if (addr == LUSR_OFF(signal))
355 			error = 1;
356 		else if (addr == LUSR_OFF(magic))
357 			error = 1;
358 		else if (addr == LUSR_OFF(u_comm))
359 			error = 1;
360 		else {
361 #ifdef DEBUG_LINUX
362 			printf("linux_ptrace: unsupported address: %d\n", addr);
363 #endif
364 			error = 1;
365 		}
366 
367 		PRELE(t);
368 
369 		error = process_write_regs(t,regs);
370 		if (error)
371 			goto out;
372 
373 		break;
374 	default:
375 		/* never reached */
376 		break;
377 	}
378 	return EIO;
379 
380 	 out:
381 	if (regs)
382 		FREE(regs, M_TEMP);
383 	if (fpregs)
384 		FREE(fpregs, M_TEMP);
385 	if (linux_regs)
386 		FREE(linux_regs, M_TEMP);
387 	if (linux_fpreg)
388 		FREE(linux_fpreg, M_TEMP);
389 	return (error);
390 }
391